#### Cleaning the Working Environment ####
rm(list = ls(all=T))
gc()

#### Getting the Working Directory ####
getwd()

#### Creating Empty Folder for Tables in the Working Directory ####
dir.create("Tables")

#### Loading Packages ####
library(haven)
library(sjPlot)
library(nnet)
library(stargazer)
library(tidyverse)
library(viridisLite)
library(viridis)
library(forcats)
library(data.table)
library(openxlsx)
library(psych)
library(lme4)
library(lattice)
library(ggeffects)
library(sjlabelled)
library(psych)
library(naniar)
library(dotwhisker)
library(broom)
library(lattice)
library(RColorBrewer)
library(merTools)
library(sjPlot)
library(ggplot2)
library(openxlsx)
library(apaTables)
library(readxl)
library(ggrepel)

#### Inserting functions ####

qcut2 <- function(x, n) {
  findInterval(x, quantile(x, seq(0, 1, length = n + 1), na.rm=T), all.inside = T)
}


#### Loading DATASETS ####
issp_2007 <- read_dta("DATASETS/ZA4850_v2-0-0.dta")
issp_2007$V5 <- as.numeric(issp_2007$V5)

#### Checking and Preparing Sleep Variables in the DATASETS ####
table(issp_2007$V67, exclude = NULL)
table(issp_2007$V68, exclude = NULL)

issp_2007$sleeping_time_hour_alternative <- substr(as.POSIXct(sprintf("%04.0f", issp_2007$V68), format='%H%M'), 12, 16)
issp_2007$getting_up_time_hour_alternative <- substr(as.POSIXct(sprintf("%04.0f", issp_2007$V67), format='%H%M'), 12, 16)

table(issp_2007$sleeping_time_hour_alternative, exclude = NULL)
table(issp_2007$getting_up_time_hour_alternative, exclude = NULL)

issp_2007$Sleeping_Hour <- format(as.POSIXct(issp_2007$sleeping_time_hour_alternative, format="%H:%M"),"%H")
issp_2007$Sleeping_Minute <- format(as.POSIXct(issp_2007$sleeping_time_hour_alternative, format="%H:%M"),"%M")

issp_2007$Waking_Hour <- format(as.POSIXct(issp_2007$getting_up_time_hour_alternative, format="%H:%M"),"%H")
issp_2007$Waking_Minute <- format(as.POSIXct(issp_2007$getting_up_time_hour_alternative, format="%H:%M"),"%M")

issp_2007$sleeping_time_hour_alternative <- as.numeric(issp_2007$Sleeping_Hour) + as.numeric(issp_2007$Sleeping_Minute)/60
issp_2007$getting_up_time_hour_alternative <- as.numeric(issp_2007$Waking_Hour) + as.numeric(issp_2007$Waking_Minute)/60

#### Loading Netherlands Dataset ####
issp_2007_netherlands <- read_dta("DATASETS/issp_2007nl_def.dta")

issp_2007_netherlands$nl_hinc <- ifelse(issp_2007_netherlands$nl_hinc==99999, NA, issp_2007_netherlands$nl_hinc)
issp_2007_netherlands$NL_INC <- issp_2007_netherlands$nl_hinc
table(issp_2007_netherlands$NL_INC, exclude = NULL)
str(issp_2007_netherlands$NL_INC)
issp_2007_netherlands$NL_INC <- as.numeric(issp_2007_netherlands$NL_INC)
table(qcut2(issp_2007_netherlands$NL_INC, 5), exclude = NULL)
issp_2007_netherlands$NL_INC_rank <- qcut2(issp_2007_netherlands$NL_INC, 5)
table(issp_2007_netherlands$NL_INC_rank, exclude = NULL)

issp_2007_netherlands$V58 <- ifelse(issp_2007_netherlands$V58==-1 | issp_2007_netherlands$V58==8, NA, issp_2007_netherlands$V58)
table(issp_2007_netherlands$V58, exclude = NULL)

issp_2007_netherlands$V66 <- ifelse(issp_2007_netherlands$V66==-1, NA, issp_2007_netherlands$V66)
table(issp_2007_netherlands$V66, exclude = NULL)

issp_2007_netherlands$V67 <- ifelse(issp_2007_netherlands$V67==-1, NA, issp_2007_netherlands$V67)
table(issp_2007_netherlands$V67, exclude = NULL)

issp_2007_netherlands$V68 <- ifelse(issp_2007_netherlands$V68==-1, NA, issp_2007_netherlands$V68)
table(issp_2007_netherlands$V68, exclude = NULL)

issp_2007_netherlands$degree <- ifelse(issp_2007_netherlands$degree==9, NA, issp_2007_netherlands$degree)
table(issp_2007_netherlands$degree, exclude = NULL)

issp_2007_netherlands$nl_hinc <- ifelse(issp_2007_netherlands$nl_hinc==99999, NA, issp_2007_netherlands$nl_hinc)
table(issp_2007_netherlands$nl_hinc, exclude = NULL)

issp_2007_netherlands$PARTY_LR <- ifelse(issp_2007_netherlands$PARTY_LR==7 | issp_2007_netherlands$PARTY_LR==9, NA, issp_2007_netherlands$PARTY_LR)
table(issp_2007_netherlands$PARTY_LR, exclude = NULL)

issp_2007_netherlands$attend <- ifelse(issp_2007_netherlands$attend==98 | issp_2007_netherlands$attend==99, NA, issp_2007_netherlands$attend)
table(issp_2007_netherlands$attend, exclude = NULL)

table(issp_2007_netherlands$relig, exclude = NULL)
issp_2007_netherlands$relig <- ifelse(issp_2007_netherlands$relig==0, NA, issp_2007_netherlands$relig)

table(issp_2007_netherlands$religgrp, exclude = NULL)
issp_2007_netherlands$religgrp <- ifelse(issp_2007_netherlands$religgrp==99, NA, issp_2007_netherlands$religgrp)

#### Checking and Preparing Sleep Variables in the Netherlands Dataset ####

issp_2007_netherlands$V67 <- as.numeric(issp_2007_netherlands$V67)
table(issp_2007_netherlands$V67, exclude = NULL)
issp_2007_netherlands$V67 <- issp_2007_netherlands$V67*100

issp_2007_netherlands$V68 <- as.numeric(issp_2007_netherlands$V68)
table(issp_2007_netherlands$V68, exclude = NULL)
issp_2007_netherlands$V68 <- issp_2007_netherlands$V68*100

issp_2007_netherlands$sleeping_time_hour_alternative <- substr(as.POSIXct(sprintf("%04.0f", issp_2007_netherlands$V68), format='%H%M'), 12, 16)
issp_2007_netherlands$getting_up_time_hour_alternative <- substr(as.POSIXct(sprintf("%04.0f", issp_2007_netherlands$V67), format='%H%M'), 12, 16)

table(issp_2007_netherlands$sleeping_time_hour_alternative, exclude = NULL)
table(issp_2007_netherlands$getting_up_time_hour_alternative, exclude = NULL)

issp_2007_netherlands$Sleeping_Hour <- format(as.POSIXct(issp_2007_netherlands$sleeping_time_hour_alternative, format="%H:%M"),"%H")
issp_2007_netherlands$Sleeping_Minute <- format(as.POSIXct(issp_2007_netherlands$sleeping_time_hour_alternative, format="%H:%M"),"%M")

issp_2007_netherlands$Waking_Hour <- format(as.POSIXct(issp_2007_netherlands$getting_up_time_hour_alternative, format="%H:%M"),"%H")
issp_2007_netherlands$Waking_Minute <- format(as.POSIXct(issp_2007_netherlands$getting_up_time_hour_alternative, format="%H:%M"),"%M")

issp_2007_netherlands$sleeping_time_hour_alternative <- as.numeric(issp_2007_netherlands$Sleeping_Hour) + as.numeric(issp_2007_netherlands$Sleeping_Minute)/60
issp_2007_netherlands$getting_up_time_hour_alternative <- as.numeric(issp_2007_netherlands$Waking_Hour) + as.numeric(issp_2007_netherlands$Waking_Minute)/60

#### Merging ISSP 2007 and the Netherlands Datasets ####
issp_2007 <- dplyr::bind_rows(issp_2007, issp_2007_netherlands)

table(issp_2007$religgrp, exclude = NULL)

issp_2007$religious_denomination <- NA
issp_2007$religious_denomination[issp_2007$religgrp==1] <- "No_religion"
issp_2007$religious_denomination[issp_2007$religgrp==2] <- "Roman_Catholic"
issp_2007$religious_denomination[issp_2007$religgrp==3] <- "Protestant"
issp_2007$religious_denomination[issp_2007$religgrp==4] <- "Christian_Orthodox"
issp_2007$religious_denomination[issp_2007$religgrp==5] <- "Jewish"
issp_2007$religious_denomination[issp_2007$religgrp==6] <- "Islam"
issp_2007$religious_denomination[issp_2007$religgrp==7] <- "Buddhism"
issp_2007$religious_denomination[issp_2007$religgrp==8] <- "Hinduism"
issp_2007$religious_denomination[issp_2007$religgrp==9] <- "Other_Christian_Religions"
issp_2007$religious_denomination[issp_2007$religgrp==10] <- "Other_Eastern_Religions"
issp_2007$religious_denomination[issp_2007$religgrp==11] <- "Other_Religions"
issp_2007$religious_denomination <- ifelse(is.na(issp_2007$religious_denomination), "Missing", issp_2007$religious_denomination)
table(issp_2007$religious_denomination, exclude = NULL)

#### Dropping Observations with Missing Sleep Variables ####
issp_2007 <- subset(issp_2007, subset = (!is.na(issp_2007$V67) | !is.na(issp_2007$V68)))

#### Calculating Duration of Sleep ####
issp_2007$sleep_duration_alternative <-  (issp_2007$getting_up_time_hour_alternative-issp_2007$sleeping_time_hour_alternative) 
table(issp_2007$sleep_duration_alternative, exclude = NULL)
issp_2007$sleep_duration_alternative <-  ifelse(issp_2007$getting_up_time_hour_alternative>=issp_2007$sleeping_time_hour_alternative, issp_2007$sleep_duration_alternative, (24 - issp_2007$sleeping_time_hour_alternative) + issp_2007$getting_up_time_hour_alternative) 
table(issp_2007$sleep_duration_alternative, exclude = NULL)

#### Calculating Chronotype ####
issp_2007$mid_point_alternative <- issp_2007$getting_up_time_hour_alternative - issp_2007$sleep_duration_alternative/2
issp_2007$mid_point_alternative <- ifelse(issp_2007$mid_point_alternative<0, issp_2007$mid_point_alternative+24, issp_2007$mid_point_alternative)
table(issp_2007$mid_point_alternative)

#### Exclusion Criteria based on Duration of Sleep and Chronotype ####
issp_2007 <- subset(issp_2007, subset = (issp_2007$sleeping_time_hour_alternative<=6 | issp_2007$sleeping_time_hour_alternative>=19) & (issp_2007$getting_up_time_hour_alternative<=15 & issp_2007$getting_up_time_hour_alternative>=4))
issp_2007 <- subset(issp_2007, subset = issp_2007$sleep_duration_alternative>=2 & sleep_duration_alternative<=18)

#### Reverse Coding Chronotype - Bigger Values for Morningness and Smaller Values for Eveningness ####
table(issp_2007$mid_point_alternative, exclude = NULL)
issp_2007$ORIGINAL_MID_POINT <- issp_2007$mid_point_alternative
issp_2007$midcenter <- issp_2007$mid_point_alternative-20
table(issp_2007$midcenter, exclude = NULL)
issp_2007$cond3 <- issp_2007$midcenter + 24
issp_2007$midcenter <- ifelse(issp_2007$midcenter<0, issp_2007$cond3, issp_2007$midcenter)
table(issp_2007$midcenter, exclude = NULL)
issp_2007$mid_point_alternative <- issp_2007$midcenter
table(issp_2007$mid_point_alternative, exclude = NULL)
issp_2007$chronotype_interval <-  issp_2007$mid_point_alternative*-1
issp_2007$sleep_duration <-  issp_2007$sleep_duration_alternative
table(issp_2007$chronotype_interval, exclude = NULL)
table(issp_2007$sleep_duration, exclude = NULL)

#### Checking and Prepating Covariates ####
table(issp_2007$degree, exclude = NULL)
issp_2007$degree <- ifelse(issp_2007$degree>5, NA, issp_2007$degree)

table(issp_2007$sex, exclude = NULL) # 1 - male, 2 - female
issp_2007$sex <- issp_2007$sex
issp_2007$sex[issp_2007$sex==1] <- 1
issp_2007$sex[issp_2007$sex==2] <- 0
table(issp_2007$sex, exclude = NULL)


table(issp_2007$V66, exclude = NULL)
issp_2007$day_off <- NA
issp_2007$day_off[issp_2007$V66==2] <- 1 ## Weekend
issp_2007$day_off[issp_2007$V66==1] <- 0 ## Weekday
table(issp_2007$day_off, exclude = NULL)


table(issp_2007$V58, exclude = NULL)
issp_2007$political_interest <- issp_2007$V58
table(issp_2007$political_interest, exclude = NULL)
issp_2007$political_interest <- ifelse(issp_2007$political_interest>4, NA, issp_2007$political_interest)
issp_2007$political_interest <- 5 - issp_2007$political_interest

table(issp_2007$urbrural, exclude = NULL)
issp_2007$urban_rural_area <- NA
issp_2007$urban_rural_area[issp_2007$urbrural==5 | issp_2007$urbrural==4] <- 1 # to match with Greece data
issp_2007$urban_rural_area[issp_2007$urbrural==3] <- 2
issp_2007$urban_rural_area[issp_2007$urbrural==2] <- 3
issp_2007$urban_rural_area[issp_2007$urbrural==1] <- 4
table(issp_2007$urban_rural_area, exclude = NULL)

table(issp_2007$attend, exclude = NULL)

issp_2007$religiosity <- NA
issp_2007$religiosity[issp_2007$attend==8] <- 1
issp_2007$religiosity[issp_2007$attend==7 | issp_2007$attend==6] <- 2
issp_2007$religiosity[issp_2007$attend==5] <- 3
issp_2007$religiosity[issp_2007$attend==4] <- 4
issp_2007$religiosity[issp_2007$attend==3] <- 5
issp_2007$religiosity[issp_2007$attend==1 | issp_2007$attend==2] <- 6
table(issp_2007$religiosity, exclude = NULL)

issp_2007$age <- ifelse(issp_2007$age<18, NA, issp_2007$age)

issp_2007$PARTY_LR <- ifelse(issp_2007$PARTY_LR>5, NA, issp_2007$PARTY_LR)
table(issp_2007$PARTY_LR, exclude = NULL)

#### Loading Greece Dataset ####

greece_data <- read_dta("DATASETS/Greece_2020.dta")

greece_data$V67 <-  greece_data$hourtime1 + greece_data$minutetime1/100
table(greece_data$V67, exclude = NULL)
greece_data$V67 <- greece_data$V67*100

greece_data$V68 <-  greece_data$hourtime2_corrected + greece_data$minutetime2/100
table(greece_data$V68, exclude = NULL)
greece_data$V68 <- greece_data$V68*100

greece_data$sleeping_time_hour_alternative <- substr(as.POSIXct(sprintf("%04.0f", greece_data$V68), format='%H%M'), 12, 16)
greece_data$getting_up_time_hour_alternative <- substr(as.POSIXct(sprintf("%04.0f", greece_data$V67), format='%H%M'), 12, 16)

table(greece_data$sleeping_time_hour_alternative, exclude = NULL)
table(greece_data$getting_up_time_hour_alternative, exclude = NULL)

greece_data$Sleeping_Hour <- format(as.POSIXct(greece_data$sleeping_time_hour_alternative, format="%H:%M"),"%H")
greece_data$Sleeping_Minute <- format(as.POSIXct(greece_data$sleeping_time_hour_alternative, format="%H:%M"),"%M")

greece_data$Waking_Hour <- format(as.POSIXct(greece_data$getting_up_time_hour_alternative, format="%H:%M"),"%H")
greece_data$Waking_Minute <- format(as.POSIXct(greece_data$getting_up_time_hour_alternative, format="%H:%M"),"%M")

greece_data$sleeping_time_hour_alternative <- as.numeric(greece_data$Sleeping_Hour) + as.numeric(greece_data$Sleeping_Minute)/60
greece_data$getting_up_time_hour_alternative <- as.numeric(greece_data$Waking_Hour) + as.numeric(greece_data$Waking_Minute)/60

greece_data$sleep_duration_alternative <-  (greece_data$getting_up_time_hour_alternative-greece_data$sleeping_time_hour_alternative) 
table(greece_data$sleep_duration_alternative, exclude = NULL)
greece_data$sleep_duration_alternative <-  ifelse(greece_data$getting_up_time_hour_alternative>=greece_data$sleeping_time_hour_alternative, greece_data$sleep_duration_alternative, (24 - greece_data$sleeping_time_hour_alternative) + greece_data$getting_up_time_hour_alternative) 
table(greece_data$sleep_duration_alternative, exclude = NULL)

greece_data$mid_point_alternative <- greece_data$getting_up_time_hour_alternative - greece_data$sleep_duration_alternative/2
greece_data$mid_point_alternative <- ifelse(greece_data$mid_point_alternative<0, greece_data$mid_point_alternative+24, greece_data$mid_point_alternative)
table(greece_data$mid_point_alternative)

greece_data <- subset(greece_data, subset = (greece_data$sleeping_time_hour_alternative<=6 | greece_data$sleeping_time_hour_alternative>=19) & (greece_data$getting_up_time_hour_alternative<=15 & greece_data$getting_up_time_hour_alternative>=4))
greece_data <- subset(greece_data, subset = greece_data$sleep_duration_alternative>=2 & sleep_duration_alternative<=18)

table(greece_data$mid_point_alternative, exclude = NULL)
greece_data$ORIGINAL_MID_POINT <- greece_data$mid_point_alternative
greece_data$midcenter <- greece_data$mid_point_alternative-20
table(greece_data$midcenter, exclude = NULL)
greece_data$cond3 <- greece_data$midcenter + 24
greece_data$midcenter <- ifelse(greece_data$midcenter<0, greece_data$cond3, greece_data$midcenter)
table(greece_data$midcenter, exclude = NULL)
greece_data$mid_point_alternative <- greece_data$midcenter
table(greece_data$mid_point_alternative, exclude = NULL)
greece_data$chronotype_interval <-  greece_data$mid_point_alternative*-1
greece_data$sleep_duration <-  greece_data$sleep_duration_alternative
table(greece_data$chronotype_interval, exclude = NULL)
table(greece_data$sleep_duration, exclude = NULL)

greece_data$degree <- NA
greece_data$degree[greece_data$D03f==1] <- 0
greece_data$degree[greece_data$D03f==2 | greece_data$D03f==3] <- 1
greece_data$degree[greece_data$D03f==4 | greece_data$D03f==5] <- 2
greece_data$degree[greece_data$D03f==6] <- 3
greece_data$degree[greece_data$D03f==7] <- 4
greece_data$degree[greece_data$D03f==8 | greece_data$D03f==9 | greece_data$D03f==10] <- 5
table(greece_data$degree, exclude = NULL)

greece_data$sex <- NA
greece_data$sex[greece_data$D02f==1] <- 1
greece_data$sex[greece_data$D02f==2] <- 0
table(greece_data$sex, exclude = NULL)

greece_data$interview_time <- paste(greece_data$qyear, greece_data$qmonth, greece_data$qday, sep = "-")
table(greece_data$interview_time, exclude = NULL)

greece_data$day_off <- NA
greece_data$day_off[greece_data$interview_time=="2019-12-14" |
                      greece_data$interview_time=="2019-12-15" |
                      greece_data$interview_time== "2019-12-22" |
                      greece_data$interview_time=="2019-12-28" |
                      greece_data$interview_time=="2019-12-29" |
                      greece_data$interview_time== "2020-1-4" |
                      greece_data$interview_time=="2020-1-5" |
                      greece_data$interview_time== "2020-2-1" |
                      greece_data$interview_time== "2020-2-2" |
                      greece_data$interview_time=="2020-2-8" |
                      greece_data$interview_time=="2020-2-9" |
                      greece_data$interview_time=="2020-2-15" |
                      greece_data$interview_time== "2020-2-16"] <- 1
greece_data$day_off <- ifelse(is.na(greece_data$day_off), 0, greece_data$day_off)
table(greece_data$day_off, exclude = NULL)

table(greece_data$Q01f, exclude = NULL)
greece_data$political_interest <- greece_data$Q01f
table(greece_data$political_interest, exclude = NULL)
greece_data$political_interest <- ifelse(greece_data$political_interest>4, NA, greece_data$political_interest)
greece_data$political_interest <- 5 - greece_data$political_interest

greece_data$urban_rural_area <- greece_data$D19f
greece_data$urban_rural_area <- ifelse(greece_data$urban_rural_area==5, NA, greece_data$urban_rural_area)
table(greece_data$urban_rural_area, exclude = NULL)

table(greece_data$D11f, exclude = NULL)
greece_data$religiosity <- greece_data$D11f
greece_data$religiosity <- ifelse(greece_data$religiosity==7, NA, greece_data$religiosity)
table(greece_data$religiosity, exclude = NULL)

table(greece_data$Q18f, exclude = NULL)
greece_data$ideology <- greece_data$Q18f
greece_data$ideology <- ifelse(greece_data$ideology==12, NA, greece_data$ideology)
table(greece_data$ideology, exclude = NULL)
greece_data$PARTY_LR <- (5-1)/(11-1)*(greece_data$ideology-11) + 5
table(greece_data$PARTY_LR, exclude = NULL)
greece_data$PARTY_LR <- round(greece_data$PARTY_LR, 0)
greece_data$PARTY_LR[greece_data$ideology==5] <- 2
greece_data$PARTY_LR[greece_data$ideology==7] <- 4
table(greece_data$PARTY_LR, greece_data$ideology, exclude = NULL)
table(greece_data$PARTY_LR, exclude = NULL)

table(greece_data$age, exclude = NULL)
greece_data$age <- ifelse(greece_data$age<18, NA, greece_data$age)
greece_data <- subset(greece_data, subset = !is.na(age))

table(greece_data$D09f, exclude = NULL)
str(greece_data$D09f)
greece_data$D09f <- as.numeric(greece_data$D09f)
greece_data$GR_INC_rank <- greece_data$D09f
greece_data$GR_INC_rank <- ifelse(greece_data$GR_INC_rank==6, NA, greece_data$GR_INC_rank)
table(greece_data$GR_INC_rank, exclude = NULL)

greece_data$V5 <- 2020

issp_2007 <- dplyr::bind_rows(issp_2007, greece_data)

#### Income variable for all ####

table(issp_2007$FI_INC, exclude = NULL)
str(issp_2007$FI_INC)
issp_2007$FI_INC <- as.numeric(issp_2007$FI_INC)
table(qcut2(issp_2007$FI_INC, 5), exclude = NULL)
issp_2007$FI_INC_rank <- qcut2(issp_2007$FI_INC, 5)
table(issp_2007$FI_INC_rank, exclude = NULL)

table(issp_2007$IE_INC, exclude = NULL)
str(issp_2007$IE_INC)
issp_2007$IE_INC <- as.numeric(issp_2007$IE_INC)
table(qcut2(issp_2007$IE_INC, 5), exclude = NULL)
issp_2007$IE_INC_rank <- qcut2(issp_2007$IE_INC, 5)
table(issp_2007$IE_INC_rank, exclude = NULL)

table(issp_2007$KR_INC, exclude = NULL)
str(issp_2007$KR_INC)
issp_2007$KR_INC <- as.numeric(issp_2007$KR_INC)
table(qcut2(issp_2007$KR_INC, 5), exclude = NULL)
issp_2007$KR_INC_rank <- qcut2(issp_2007$KR_INC, 5)
table(issp_2007$KR_INC_rank, exclude = NULL)

table(issp_2007$MX_INC, exclude = NULL)
str(issp_2007$MX_INC)
issp_2007$MX_INC <- as.numeric(issp_2007$MX_INC)
table(qcut2(issp_2007$MX_INC, 5), exclude = NULL)
issp_2007$MX_INC_rank <- qcut2(issp_2007$MX_INC, 5)
table(issp_2007$MX_INC_rank, exclude = NULL)

table(issp_2007$PH_INC, exclude = NULL)
str(issp_2007$PH_INC)
issp_2007$PH_INC <- as.numeric(issp_2007$PH_INC)
table(qcut2(issp_2007$PH_INC, 5), exclude = NULL)
issp_2007$PH_INC_rank <- qcut2(issp_2007$PH_INC, 5)
table(issp_2007$PH_INC_rank, exclude = NULL)

table(issp_2007$RU_INC, exclude = NULL)
str(issp_2007$RU_INC)
issp_2007$RU_INC <- as.numeric(issp_2007$RU_INC)
table(qcut2(issp_2007$RU_INC, 5), exclude = NULL)
issp_2007$RU_INC_rank <- qcut2(issp_2007$RU_INC, 5)
table(issp_2007$RU_INC_rank, exclude = NULL)

table(issp_2007$CH_INC, exclude = NULL)
str(issp_2007$CH_INC)
issp_2007$CH_INC <- as.numeric(issp_2007$CH_INC)
table(qcut2(issp_2007$CH_INC, 5), exclude = NULL)
issp_2007$CH_INC_rank <- qcut2(issp_2007$CH_INC, 5)
table(issp_2007$CH_INC_rank, exclude = NULL)

table(issp_2007$NZ_INC, exclude = NULL)
str(issp_2007$NZ_INC)
issp_2007$NZ_INC <- as.numeric(issp_2007$NZ_INC)
table(qcut2(issp_2007$NZ_INC, 5), exclude = NULL)
issp_2007$NZ_INC_rank <- qcut2(issp_2007$NZ_INC, 5)
table(issp_2007$NZ_INC_rank, exclude = NULL)

issp_2007$income <- rowSums(issp_2007[,c("FI_INC_rank", 
                                         "IE_INC_rank",
                                         "KR_INC_rank",
                                         "MX_INC_rank",
                                         "NL_INC_rank",
                                         "NZ_INC_rank",
                                         "PH_INC_rank",
                                         "RU_INC_rank",
                                         "CH_INC_rank",
                                         "GR_INC_rank")], na.rm = T)
table(issp_2007$income, exclude = NULL)
table(issp_2007$income, issp_2007$FI_INC_rank, exclude = NULL)
table(issp_2007$income, issp_2007$IE_INC_rank, exclude = NULL)
table(issp_2007$income, issp_2007$KR_INC_rank, exclude = NULL)
table(issp_2007$income, issp_2007$MX_INC_rank, exclude = NULL)
table(issp_2007$income, issp_2007$NL_INC_rank, exclude = NULL)
table(issp_2007$income, issp_2007$NZ_INC_rank, exclude = NULL)
table(issp_2007$income, issp_2007$PH_INC_rank, exclude = NULL)
table(issp_2007$income, issp_2007$RU_INC_rank, exclude = NULL)
table(issp_2007$income, issp_2007$CH_INC_rank, exclude = NULL)
table(issp_2007$income, issp_2007$GR_INC_rank, exclude = NULL)
issp_2007$income <- ifelse(issp_2007$income==0, NA, issp_2007$income)
table(issp_2007$income, exclude = NULL)

table(issp_2007$V5, exclude = NULL)
issp_2007$country <- NA
issp_2007$country[issp_2007$V5==246] <- 'Finland'
issp_2007$country[issp_2007$V5==2020] <- 'Greece'
issp_2007$country[issp_2007$V5==372] <- 'Ireland'
issp_2007$country[issp_2007$V5==410] <- 'South Korea'
issp_2007$country[issp_2007$V5==484] <- 'Mexico'
issp_2007$country[issp_2007$V5==528] <- 'the Netherlands'
issp_2007$country[issp_2007$V5==554] <- 'New Zealand'
issp_2007$country[issp_2007$V5==608] <- 'the Philippines'
issp_2007$country[issp_2007$V5==643] <- 'Russia'
issp_2007$country[issp_2007$V5==756] <- 'Switzerland'
table(issp_2007$country, exclude = NULL)
issp_2007$country <- factor(issp_2007$country, 
                            levels = c('Finland','Greece', 'Ireland', 'Mexico', 'the Netherlands', 'New Zealand', 'the Philippines', 'Russia', 'South Korea', 'Switzerland'))
table(issp_2007$country, exclude = NULL)


issp_2007 <- subset(issp_2007, subset = country == 'Finland' | 
                      country == 'Greece' |
                      country == 'Ireland' | 
                      country == 'South Korea' | 
                      country == 'Mexico' | 
                      country == 'the Netherlands' | 
                      country == 'New Zealand' | 
                      country == 'the Philippines' | country == 'Russia' | country == 'Switzerland')

#### Getting Country-Region Level Variables ####
table(issp_2007$IE_REG, exclude = NULL)
table(issp_2007$FI_REG, exclude = NULL)
table(issp_2007$KR_REG, exclude = NULL)
table(issp_2007$MX_REG, exclude = NULL)
table(issp_2007$NL_REG, exclude = NULL)
table(issp_2007$NZ_REG, exclude = NULL)
table(issp_2007$PH_REG, exclude = NULL)
table(issp_2007$RU_REG, exclude = NULL)
table(issp_2007$CH_REG, exclude = NULL)

issp_2007$region_id <- NA
issp_2007$region_id <- rowSums( issp_2007[,c("IE_REG", "FI_REG", "KR_REG",
                                             "MX_REG", "NL_REG", "NZ_REG",
                                             "PH_REG", "RU_REG", "CH_REG")], na.rm = T)

table(issp_2007$region_id, exclude = NULL)

issp_2007$region_id <- ifelse(issp_2007$region_id==0, NA, issp_2007$region_id)

issp_2007$region_id_merging <- paste(issp_2007$V5, issp_2007$region_id)
table(issp_2007$region_id_merging, exclude = NULL)  

geocodes_temperatures <- read_excel("Geographic Dataset/geocodes_temperatures.xlsx")
geocodes_temperatures$region_id_merging <- paste(geocodes_temperatures$V5, geocodes_temperatures$region_id)
table(geocodes_temperatures$region_id_merging, exclude = NULL) 

issp_2007 <- merge(issp_2007,geocodes_temperatures,by="region_id_merging", all = T)

#### Pooled Data - Grand and group mean centering ####

issp_2007 <- subset(issp_2007, select=c("country",
                           "region_id_merging",
                           "V3",
                           "PARTY_LR",
                           "religiosity",
                           "chronotype_interval",
                           "day_off",
                           "urban_rural_area",
                           "age",
                           "degree",
                           "sex",
                           "political_interest",
                           "income",
                           "religious_denomination",
                           "latitude",
                           "longitude",
                           "mean_temperature",
                           "mid_field_season",
                           "CH_PRTY",
                           "FI_PRTY", 
                           "IE_PRTY",
                           "KR_PRTY", 
                           "MX_PRTY",
                           "NZ_PRTY",
                           "PH_PRTY", 
                           "RU_PRTY",
                           "nl_prty",
                           "sun_rise_local",
                           "solar_noon_local",
                           "sun_set_local",
                           "day_time_duration",
                           "ORIGINAL_MID_POINT"))

table(issp_2007$country, exclude = NULL)

issp_2007$voted_parties <- NA
issp_2007$voted_parties[issp_2007$country=="Greece"] <- "GR - Missing"
table(issp_2007$nl_prty, exclude = NULL)
issp_2007$voted_parties[issp_2007$country=="the Netherlands" & issp_2007$nl_prty==1] <- "NL - CDA"
issp_2007$voted_parties[issp_2007$country=="the Netherlands" & issp_2007$nl_prty==2] <- "NL - PvdA"
issp_2007$voted_parties[issp_2007$country=="the Netherlands" & issp_2007$nl_prty==3] <- "NL - VVD"
issp_2007$voted_parties[issp_2007$country=="the Netherlands" & issp_2007$nl_prty==4] <- "NL - SP"
issp_2007$voted_parties[issp_2007$country=="the Netherlands" & issp_2007$nl_prty==5] <- "NL - Groen Links"
issp_2007$voted_parties[issp_2007$country=="the Netherlands" & issp_2007$nl_prty==7] <- "NL - D66"
issp_2007$voted_parties[issp_2007$country=="the Netherlands" & issp_2007$nl_prty==8] <- "NL - Wilders-PVV"
issp_2007$voted_parties[issp_2007$country=="the Netherlands" & issp_2007$nl_prty==9] <- "NL - ChristenUnie"
issp_2007$voted_parties[issp_2007$country=="the Netherlands" & issp_2007$nl_prty==10] <- "NL - SGP"
issp_2007$voted_parties[issp_2007$country=="the Netherlands" & issp_2007$nl_prty==11] <- "NL - Partij van de Dieren"
issp_2007$voted_parties[issp_2007$country=="the Netherlands" & issp_2007$nl_prty==12] <- "NL - Lijst Verdonk Trots op Nederland"
issp_2007$voted_parties[issp_2007$country=="the Netherlands" & issp_2007$nl_prty==13] <- "NL - Other party"
issp_2007$voted_parties[issp_2007$country=="the Netherlands" & issp_2007$nl_prty==14] <- "NL - Would not vote"
issp_2007$voted_parties[issp_2007$country=="the Netherlands" & issp_2007$nl_prty==99] <- "NL - Missing"
table(issp_2007$CH_PRTY, exclude = NULL)
issp_2007$voted_parties[issp_2007$country=="Switzerland" & issp_2007$CH_PRTY==1] <- "CH - Christian Democratic Party"
issp_2007$voted_parties[issp_2007$country=="Switzerland" & issp_2007$CH_PRTY==2] <- "CH - Evangelical Peoples Party"
issp_2007$voted_parties[issp_2007$country=="Switzerland" & issp_2007$CH_PRTY==3] <- "CH - Radical Party"
issp_2007$voted_parties[issp_2007$country=="Switzerland" & issp_2007$CH_PRTY==4] <- "CH - Social Democratic Party"
issp_2007$voted_parties[issp_2007$country=="Switzerland" & issp_2007$CH_PRTY==5] <- "CH - Swiss Peoples Party"
issp_2007$voted_parties[issp_2007$country=="Switzerland" & issp_2007$CH_PRTY==7] <- "CH - Liberal Party"
issp_2007$voted_parties[issp_2007$country=="Switzerland" & issp_2007$CH_PRTY==8] <- "CH - Labour Party"
issp_2007$voted_parties[issp_2007$country=="Switzerland" & issp_2007$CH_PRTY==9] <- "CH - Swiss Democrats"
issp_2007$voted_parties[issp_2007$country=="Switzerland" & issp_2007$CH_PRTY==10] <- "CH - Green Party"
issp_2007$voted_parties[issp_2007$country=="Switzerland" & issp_2007$CH_PRTY==11] <- "CH - Freedom Party"
issp_2007$voted_parties[issp_2007$country=="Switzerland" & issp_2007$CH_PRTY==95] <- "CH - Other party"
issp_2007$voted_parties <- ifelse(issp_2007$country=="Switzerland" & is.na(issp_2007$voted_parties), "CH - Missing", issp_2007$voted_parties)
table(issp_2007$FI_PRTY, exclude = NULL)
issp_2007$voted_parties[issp_2007$country=="Finland" & issp_2007$FI_PRTY==1] <- "FI - Social Democr Party"
issp_2007$voted_parties[issp_2007$country=="Finland" & issp_2007$FI_PRTY==2] <- "FI - Centre Party of FIN"
issp_2007$voted_parties[issp_2007$country=="Finland" & issp_2007$FI_PRTY==3] <- "FI - Nat Coalition Party"
issp_2007$voted_parties[issp_2007$country=="Finland" & issp_2007$FI_PRTY==4] <- "FI - Left Alliance"
issp_2007$voted_parties[issp_2007$country=="Finland" & issp_2007$FI_PRTY==5] <- "FI - Swedish Peoples Prty"
issp_2007$voted_parties[issp_2007$country=="Finland" & issp_2007$FI_PRTY==6] <- "FI - Green League"
issp_2007$voted_parties[issp_2007$country=="Finland" & issp_2007$FI_PRTY==7] <- "FI - Christian League"
issp_2007$voted_parties[issp_2007$country=="Finland" & issp_2007$FI_PRTY==8] <- "FI - True Finns"
issp_2007$voted_parties[issp_2007$country=="Finland" & issp_2007$FI_PRTY==95] <- "FI - Other Party"
issp_2007$voted_parties <- ifelse(issp_2007$country=="Finland" & is.na(issp_2007$voted_parties), "FI - Missing", issp_2007$voted_parties)
table(issp_2007$IE_PRTY, exclude = NULL)
issp_2007$voted_parties[issp_2007$country=="Ireland" & issp_2007$IE_PRTY==1] <- "IE - Fianna Fail"
issp_2007$voted_parties[issp_2007$country=="Ireland" & issp_2007$IE_PRTY==2] <- "IE - Fine Gael"
issp_2007$voted_parties[issp_2007$country=="Ireland" & issp_2007$IE_PRTY==3] <- "IE - Labour"
issp_2007$voted_parties[issp_2007$country=="Ireland" & issp_2007$IE_PRTY==4] <- "IE - Progressive Democrats"
issp_2007$voted_parties[issp_2007$country=="Ireland" & issp_2007$IE_PRTY==5] <- "IE - Green Party"
issp_2007$voted_parties[issp_2007$country=="Ireland" & issp_2007$IE_PRTY==7] <- "IE - Sinn Fein"
issp_2007$voted_parties[issp_2007$country=="Ireland" & issp_2007$IE_PRTY==95] <- "IE - Other party"
issp_2007$voted_parties <- ifelse(issp_2007$country=="Ireland" & is.na(issp_2007$voted_parties), "IE - Missing", issp_2007$voted_parties)
table(issp_2007$KR_PRTY, exclude = NULL)
issp_2007$voted_parties[issp_2007$country=="South Korea" & issp_2007$KR_PRTY==1] <- "KR - People First Party"
issp_2007$voted_parties[issp_2007$country=="South Korea" & issp_2007$KR_PRTY==2] <- "KR - Democratic Labor Party"
issp_2007$voted_parties[issp_2007$country=="South Korea" & issp_2007$KR_PRTY==3] <- "KR - Uri Party"
issp_2007$voted_parties[issp_2007$country=="South Korea" & issp_2007$KR_PRTY==4] <- "KR - Democratic Party"
issp_2007$voted_parties[issp_2007$country=="South Korea" & issp_2007$KR_PRTY==5] <- "KR - Grand National Party"
issp_2007$voted_parties[issp_2007$country=="South Korea" & issp_2007$KR_PRTY==95] <- "KR - Other party"
issp_2007$voted_parties <- ifelse(issp_2007$country=="South Korea" & is.na(issp_2007$voted_parties), "KR - Missing", issp_2007$voted_parties)
table(issp_2007$MX_PRTY, exclude = NULL)
issp_2007$voted_parties[issp_2007$country=="Mexico" & issp_2007$MX_PRTY==1] <- "MX - pan"
issp_2007$voted_parties[issp_2007$country=="Mexico" & issp_2007$MX_PRTY==2] <- "MX - pri"
issp_2007$voted_parties[issp_2007$country=="Mexico" & issp_2007$MX_PRTY==3] <- "MX - prd"
issp_2007$voted_parties[issp_2007$country=="Mexico" & issp_2007$MX_PRTY==4] <- "MX - pt"
issp_2007$voted_parties[issp_2007$country=="Mexico" & issp_2007$MX_PRTY==5] <- "MX - pvem"
issp_2007$voted_parties[issp_2007$country=="Mexico" & issp_2007$MX_PRTY==6] <- "MX - Convergencia"
issp_2007$voted_parties[issp_2007$country=="Mexico" & issp_2007$MX_PRTY==8] <- "MX - panal"
issp_2007$voted_parties[issp_2007$country=="Mexico" & issp_2007$MX_PRTY==95] <- "MX - Other party"
issp_2007$voted_parties <- ifelse(issp_2007$country=="Mexico" & is.na(issp_2007$voted_parties), "MX - Missing", issp_2007$voted_parties)
table(issp_2007$NZ_PRTY, exclude = NULL)
issp_2007$voted_parties[issp_2007$country=="New Zealand" & issp_2007$NZ_PRTY==1] <- "NZ - Act"
issp_2007$voted_parties[issp_2007$country=="New Zealand" & issp_2007$NZ_PRTY==2] <- "NZ - Alliance"
issp_2007$voted_parties[issp_2007$country=="New Zealand" & issp_2007$NZ_PRTY==3] <- "NZ - Green"
issp_2007$voted_parties[issp_2007$country=="New Zealand" & issp_2007$NZ_PRTY==4] <- "NZ - Labour"
issp_2007$voted_parties[issp_2007$country=="New Zealand" & issp_2007$NZ_PRTY==5] <- "NZ - National"
issp_2007$voted_parties[issp_2007$country=="New Zealand" & issp_2007$NZ_PRTY==6] <- "NZ - NZ First"
issp_2007$voted_parties[issp_2007$country=="New Zealand" & issp_2007$NZ_PRTY==7] <- "NZ - Progressive Coalition"
issp_2007$voted_parties[issp_2007$country=="New Zealand" & issp_2007$NZ_PRTY==8] <- "NZ - United Future"
issp_2007$voted_parties[issp_2007$country=="New Zealand" & issp_2007$NZ_PRTY==9] <- "NZ - Maori Party"
issp_2007$voted_parties[issp_2007$country=="New Zealand" & issp_2007$NZ_PRTY==95] <- "NZ - Other party"
issp_2007$voted_parties <- ifelse(issp_2007$country=="New Zealand" & is.na(issp_2007$voted_parties), "NZ - Missing", issp_2007$voted_parties)
table(issp_2007$PH_PRTY, exclude = NULL)
issp_2007$voted_parties[issp_2007$country=="the Philippines" & issp_2007$PH_PRTY==1] <- "PH - nationalista"
issp_2007$voted_parties[issp_2007$country=="the Philippines" & issp_2007$PH_PRTY==3] <- "PH - OPOSISYON/OPPOSITION"
issp_2007$voted_parties[issp_2007$country=="the Philippines" & issp_2007$PH_PRTY==4] <- "PH - LIBERAL PARTY"
issp_2007$voted_parties[issp_2007$country=="the Philippines" & issp_2007$PH_PRTY==5] <- "PH - kampi"
issp_2007$voted_parties[issp_2007$country=="the Philippines" & issp_2007$PH_PRTY==6] <- "PH - independent"
issp_2007$voted_parties[issp_2007$country=="the Philippines" & issp_2007$PH_PRTY==8] <- "PH - PARTIDO NINA TRILLANES AT ESCUDERO"
issp_2007$voted_parties[issp_2007$country=="the Philippines" & issp_2007$PH_PRTY==9] <- "PH - TEAM UNITY"
issp_2007$voted_parties[issp_2007$country=="the Philippines" & issp_2007$PH_PRTY==10] <- "PH - administration"
issp_2007$voted_parties[issp_2007$country=="the Philippines" & issp_2007$PH_PRTY==13] <- "PH - PARTIDO NI ERAP"
issp_2007$voted_parties[issp_2007$country=="the Philippines" & issp_2007$PH_PRTY==14] <- "PH - akbayan"
issp_2007$voted_parties[issp_2007$country=="the Philippines" & issp_2007$PH_PRTY==15] <- "PH - FPJ PM - FERNANDO POE JR. - PARTIDONG MASA"
issp_2007$voted_parties[issp_2007$country=="the Philippines" & issp_2007$PH_PRTY==19] <- "PH - LAKAS-NUCD-CMD"
issp_2007$voted_parties[issp_2007$country=="the Philippines" & issp_2007$PH_PRTY==24] <- "PH - buhay"
issp_2007$voted_parties[issp_2007$country=="the Philippines" & issp_2007$PH_PRTY==28] <- "PH - cibac"
issp_2007$voted_parties[issp_2007$country=="the Philippines" & issp_2007$PH_PRTY==29] <- "PH - MASANG PILIPINO"
issp_2007$voted_parties[issp_2007$country=="the Philippines" & issp_2007$PH_PRTY==30] <- "PH - anakbayan"
issp_2007$voted_parties[issp_2007$country=="the Philippines" & issp_2007$PH_PRTY==31] <- "PH - BAYAN MUNA"
issp_2007$voted_parties[issp_2007$country=="the Philippines" & issp_2007$PH_PRTY==33] <- "PH - laban"
issp_2007$voted_parties[issp_2007$country=="the Philippines" & issp_2007$PH_PRTY==36] <- "PH - lakas"
issp_2007$voted_parties[issp_2007$country=="the Philippines" & issp_2007$PH_PRTY==38] <- "PH - lacson"
issp_2007$voted_parties[issp_2007$country=="the Philippines" & issp_2007$PH_PRTY==39] <- "PH - BOPK - BANDO OSME�A PUNDOK KAUSWAGAN"
issp_2007$voted_parties[issp_2007$country=="the Philippines" & issp_2007$PH_PRTY==41] <- "PH - kababaihan"
issp_2007$voted_parties[issp_2007$country=="the Philippines" & issp_2007$PH_PRTY==46] <- "PH - PARTIDO NI MAYOR CONSTANTINO"
issp_2007$voted_parties[issp_2007$country=="the Philippines" & issp_2007$PH_PRTY==47] <- "PH - rightess"
issp_2007$voted_parties[issp_2007$country=="the Philippines" & issp_2007$PH_PRTY==48] <- "PH - neutral"
issp_2007$voted_parties[issp_2007$country=="the Philippines" & issp_2007$PH_PRTY==49] <- "PH - ANAK PAWIS"
issp_2007$voted_parties[issp_2007$country=="the Philippines" & issp_2007$PH_PRTY==50] <- "PH - NPC-NATIONAL PEOPLE COALITION"
issp_2007$voted_parties[issp_2007$country=="the Philippines" & issp_2007$PH_PRTY==51] <- "PH - OMPIA PARTY"
issp_2007$voted_parties[issp_2007$country=="the Philippines" & issp_2007$PH_PRTY==52] <- "PH - UMMAH PARTY"
issp_2007$voted_parties[issp_2007$country=="the Philippines" & issp_2007$PH_PRTY==53] <- "PH - PARTIDO NI CORY AQUINO"
issp_2007$voted_parties[issp_2007$country=="the Philippines" & issp_2007$PH_PRTY==54] <- "PH - LABAN SA PARTIDO NI GLORIA"
issp_2007$voted_parties[issp_2007$country=="the Philippines" & issp_2007$PH_PRTY==55] <- "PH - LABAN NG DEMOKRATIKONG PILIPINO"
issp_2007$voted_parties[issp_2007$country=="the Philippines" & issp_2007$PH_PRTY==56] <- "PH - PARTIDO NI JUN LOZADA"
issp_2007$voted_parties[issp_2007$country=="the Philippines" & issp_2007$PH_PRTY==57] <- "PH - PARTIDO NI SILVERIO"
issp_2007$voted_parties[issp_2007$country=="the Philippines" & issp_2007$PH_PRTY==58] <- "PH - amante"
issp_2007$voted_parties[issp_2007$country=="the Philippines" & issp_2007$PH_PRTY==59] <- "PH - poverty"
issp_2007$voted_parties[issp_2007$country=="the Philippines" & issp_2007$PH_PRTY==60] <- "PH - PARTIDO NI GOV.MIGS DOMINGUEZ (SARANGGANI)"
issp_2007$voted_parties[issp_2007$country=="the Philippines" & issp_2007$PH_PRTY==61] <- "PH - PARTIDO NI MAYOR AGUILA"
issp_2007$voted_parties[issp_2007$country=="the Philippines" & issp_2007$PH_PRTY==62] <- "PH - DUTERTE PARA SA MASA"
issp_2007$voted_parties[issp_2007$country=="the Philippines" & issp_2007$PH_PRTY==63] <- "PH - SAMAHANG WARAY"
issp_2007$voted_parties[issp_2007$country=="the Philippines" & issp_2007$PH_PRTY==64] <- "PH - KILUSAN NG MASANG PILIPINO"
issp_2007$voted_parties[issp_2007$country=="the Philippines" & issp_2007$PH_PRTY==65] <- "PH - CHRISTIAN MUSLIM DEMOCRATIC FEDERATION"
issp_2007$voted_parties[issp_2007$country=="the Philippines" & issp_2007$PH_PRTY==66] <- "PH - THE TEACHER"
issp_2007$voted_parties <- ifelse(issp_2007$country=="the Philippines" & is.na(issp_2007$voted_parties), "PH - Missing", issp_2007$voted_parties)
table(issp_2007$RU_PRTY, exclude = NULL)
issp_2007$voted_parties[issp_2007$country=="Russia" & issp_2007$RU_PRTY==1] <- "RU - Pensioners Party/Party of Social Justice"
issp_2007$voted_parties[issp_2007$country=="Russia" & issp_2007$RU_PRTY==2] <- "RU - Union of right forces"
issp_2007$voted_parties[issp_2007$country=="Russia" & issp_2007$RU_PRTY==3] <- "RU - Yabloko"
issp_2007$voted_parties[issp_2007$country=="Russia" & issp_2007$RU_PRTY==4] <- "RU - Russian Ecological Party �Green� (Panfilov A.)"
issp_2007$voted_parties[issp_2007$country=="Russia" & issp_2007$RU_PRTY==5] <- "RU - Country Party of Russia (Lapshin M.)"
issp_2007$voted_parties[issp_2007$country=="Russia" & issp_2007$RU_PRTY==7] <- "RU - Party of Revival/Russian Party of Life (Seleznev G./ Miro"
issp_2007$voted_parties[issp_2007$country=="Russia" & issp_2007$RU_PRTY==8] <- "RU - Motherland (Glazyev S.)"
issp_2007$voted_parties[issp_2007$country=="Russia" & issp_2007$RU_PRTY==9] <- "RU - Liberal Democratic Party of Russia (Ghirinovsky V.)"
issp_2007$voted_parties[issp_2007$country=="Russia" & issp_2007$RU_PRTY==10] <- "RU - United Russia (Gryzlov B.)"
issp_2007$voted_parties[issp_2007$country=="Russia" & issp_2007$RU_PRTY==11] <- "RU - Communist Party of Russian Federation (Zyuganov G.)"
issp_2007$voted_parties[issp_2007$country=="Russia" & issp_2007$RU_PRTY==95] <- "RU - Other party"
issp_2007$voted_parties[issp_2007$country=="Russia" & issp_2007$RU_PRTY==96] <- "RU - Against all/ threw out/ damaged voting paper"
issp_2007$voted_parties <- ifelse(issp_2007$country=="Russia" & is.na(issp_2007$voted_parties), "RU - Missing", issp_2007$voted_parties)
table(issp_2007$voted_parties, exclude = NULL)

table(issp_2007$religious_denomination, exclude = NULL)
issp_2007$religious_denomination <- ifelse(is.na(issp_2007$religious_denomination), "Missing", issp_2007$religious_denomination)

table(issp_2007$latitude, exclude = NULL)
issp_2007$latitude <- ifelse(is.na(issp_2007$latitude), -999999, issp_2007$latitude)

table(issp_2007$longitude, exclude = NULL)
issp_2007$longitude <- ifelse(is.na(issp_2007$longitude), -999999, issp_2007$longitude)

table(issp_2007$mean_temperature, exclude = NULL)
issp_2007$mean_temperature <- ifelse(is.na(issp_2007$mean_temperature), -999999, issp_2007$mean_temperature)

table(issp_2007$mid_field_season, exclude = NULL)
issp_2007$mid_field_season <- ifelse(is.na(issp_2007$mid_field_season), "Missing", issp_2007$mid_field_season)

issp_2007$latitude <- ifelse(issp_2007$latitude == -999999, NA, issp_2007$latitude)
issp_2007$longitude <- ifelse(issp_2007$longitude == -999999, NA, issp_2007$longitude)
issp_2007$mean_temperature <- ifelse(issp_2007$mean_temperature == -999999, NA, issp_2007$mean_temperature)
issp_2007$mid_field_season <- ifelse(issp_2007$mid_field_season == "Missing", NA, issp_2007$mid_field_season)

issp_2007 <- fastDummies::dummy_cols(issp_2007, select_columns = 'religious_denomination')

issp_2007$PARTY_LR_avg <- mean(issp_2007$PARTY_LR, na.rm = T)
issp_2007$PARTY_LR_sd <- sd(issp_2007$PARTY_LR, na.rm = T)
issp_2007$PARTY_LR_2sd <- issp_2007$PARTY_LR_sd*2
issp_2007$PARTY_LR_beta <- (issp_2007$PARTY_LR-issp_2007$PARTY_LR_avg)/issp_2007$PARTY_LR_2sd
table(issp_2007$PARTY_LR_beta, exclude = NULL)

issp_2007$religiosity_avg <- mean(issp_2007$religiosity, na.rm = T)
issp_2007$religiosity_sd <- sd(issp_2007$religiosity, na.rm = T)
issp_2007$religiosity_2sd <- issp_2007$religiosity_sd*2
issp_2007$religiosity_beta <- (issp_2007$religiosity-issp_2007$religiosity_avg)/issp_2007$religiosity_2sd
table(issp_2007$religiosity_beta, exclude = NULL)

issp_2007 <- issp_2007 %>%
  group_by(country) %>%
  mutate(group_mean_chronotype = mean(chronotype_interval, na.rm = T),
         group_sd_chronotype = sd(chronotype_interval, na.rm = T))

issp_2007$chronotype_interval_avg <- issp_2007$group_mean_chronotype
issp_2007$chronotype_interval_sd <- issp_2007$group_sd_chronotype
issp_2007$chronotype_interval_2sd <- issp_2007$chronotype_interval_sd*2
issp_2007$chronotype_interval_beta <- (issp_2007$chronotype_interval-issp_2007$chronotype_interval_avg)/issp_2007$chronotype_interval_2sd

issp_2007$day_off_avg <- mean(issp_2007$day_off, na.rm = T)
issp_2007$day_off_beta <- (issp_2007$day_off-issp_2007$day_off_avg)

table(issp_2007$urban_rural_area, exclude = NULL)
issp_2007$urban_rural_area_avg <- mean(issp_2007$urban_rural_area, na.rm = T)
issp_2007$urban_rural_area_sd <- sd(issp_2007$urban_rural_area, na.rm = T)
issp_2007$urban_rural_area_2sd <- issp_2007$urban_rural_area_sd*2
issp_2007$urban_rural_area_beta <- (issp_2007$urban_rural_area-issp_2007$urban_rural_area_avg)/issp_2007$urban_rural_area_2sd

table(issp_2007$age, exclude = NULL)
issp_2007$age_avg <- mean(issp_2007$age, na.rm = T)
issp_2007$age_sd <- sd(issp_2007$age, na.rm = T)
issp_2007$age_2sd <- issp_2007$age_sd*2
issp_2007$age_beta <- (issp_2007$age-issp_2007$age_avg)/issp_2007$age_2sd

table(issp_2007$degree, exclude = NULL)
issp_2007$degree_avg <- mean(issp_2007$degree, na.rm = T)
issp_2007$degree_sd <- sd(issp_2007$degree, na.rm = T)
issp_2007$degree_2sd <- issp_2007$degree_sd*2
issp_2007$degree_beta <- (issp_2007$degree-issp_2007$degree_avg)/issp_2007$degree_2sd

issp_2007$sex_dummy_avg <- mean(issp_2007$sex, na.rm = T)
issp_2007$sex_beta <- (issp_2007$sex-issp_2007$sex_dummy_avg)

issp_2007$political_interest_avg <- mean(issp_2007$political_interest, na.rm = T)
issp_2007$political_interest_sd <- sd(issp_2007$political_interest, na.rm = T)
issp_2007$political_interest_2sd <- issp_2007$political_interest_sd*2
issp_2007$political_interest_beta <- (issp_2007$political_interest-issp_2007$political_interest_avg)/issp_2007$political_interest_2sd

issp_2007$income_avg <- mean(issp_2007$income, na.rm = T)
issp_2007$income_sd <- sd(issp_2007$income, na.rm = T)
issp_2007$income_2sd <- issp_2007$income_sd*2
issp_2007$income_beta <- (issp_2007$income-issp_2007$income_avg)/issp_2007$income_2sd

table(issp_2007$religious_denomination_Buddhism, exclude = NULL)
issp_2007$religious_denomination_Buddhism_avg <- mean(issp_2007$religious_denomination_Buddhism, na.rm = T)
issp_2007$religious_denomination_Buddhism_beta <- (issp_2007$religious_denomination_Buddhism-issp_2007$religious_denomination_Buddhism_avg)
table(issp_2007$religious_denomination_Buddhism_beta, exclude = NULL)

table(issp_2007$religious_denomination_Christian_Orthodox, exclude = NULL)
issp_2007$religious_denomination_Christian_Orthodox_avg <- mean(issp_2007$religious_denomination_Christian_Orthodox, na.rm = T)
issp_2007$religious_denomination_Christian_Orthodox_beta <- (issp_2007$religious_denomination_Christian_Orthodox-issp_2007$religious_denomination_Christian_Orthodox_avg)
table(issp_2007$religious_denomination_Christian_Orthodox_beta, exclude = NULL)

table(issp_2007$religious_denomination_Hinduism, exclude = NULL)
issp_2007$religious_denomination_Hinduism_avg <- mean(issp_2007$religious_denomination_Hinduism, na.rm = T)
issp_2007$religious_denomination_Hinduism_beta <- (issp_2007$religious_denomination_Hinduism-issp_2007$religious_denomination_Hinduism_avg)
table(issp_2007$religious_denomination_Hinduism_beta, exclude = NULL)

table(issp_2007$religious_denomination_Islam, exclude = NULL)
issp_2007$religious_denomination_Islam_avg <- mean(issp_2007$religious_denomination_Islam, na.rm = T)
issp_2007$religious_denomination_Islam_beta <- (issp_2007$religious_denomination_Islam-issp_2007$religious_denomination_Islam_avg)
table(issp_2007$religious_denomination_Islam_beta, exclude = NULL)

table(issp_2007$religious_denomination_Jewish, exclude = NULL)
issp_2007$religious_denomination_Jewish_avg <- mean(issp_2007$religious_denomination_Jewish, na.rm = T)
issp_2007$religious_denomination_Jewish_beta <- (issp_2007$religious_denomination_Jewish-issp_2007$religious_denomination_Jewish_avg)
table(issp_2007$religious_denomination_Jewish_beta, exclude = NULL)

table(issp_2007$religious_denomination_No_religion, exclude = NULL)
issp_2007$religious_denomination_No_religion_avg <- mean(issp_2007$religious_denomination_No_religion, na.rm = T)
issp_2007$religious_denomination_No_religion_beta <- (issp_2007$religious_denomination_No_religion-issp_2007$religious_denomination_No_religion_avg)
table(issp_2007$religious_denomination_No_religion_beta, exclude = NULL)

table(issp_2007$religious_denomination_Other_Christian_Religions, exclude = NULL)
issp_2007$religious_denomination_Other_Christian_Religions_avg <- mean(issp_2007$religious_denomination_Other_Christian_Religions, na.rm = T)
issp_2007$religious_denomination_Other_Christian_Religions_beta <- (issp_2007$religious_denomination_Other_Christian_Religions-issp_2007$religious_denomination_Other_Christian_Religions_avg)
table(issp_2007$religious_denomination_Other_Christian_Religions_beta, exclude = NULL)

table(issp_2007$religious_denomination_Other_Eastern_Religions, exclude = NULL)
issp_2007$religious_denomination_Other_Eastern_Religions_avg <- mean(issp_2007$religious_denomination_Other_Eastern_Religions, na.rm = T)
issp_2007$religious_denomination_Other_Eastern_Religions_beta <- (issp_2007$religious_denomination_Other_Eastern_Religions-issp_2007$religious_denomination_Other_Eastern_Religions_avg)
table(issp_2007$religious_denomination_Other_Eastern_Religions_beta, exclude = NULL)

table(issp_2007$religious_denomination_Other_Religions, exclude = NULL)
issp_2007$religious_denomination_Other_Religions_avg <- mean(issp_2007$religious_denomination_Other_Religions, na.rm = T)
issp_2007$religious_denomination_Other_Religions_beta <- (issp_2007$religious_denomination_Other_Religions-issp_2007$religious_denomination_Other_Religions_avg)
table(issp_2007$religious_denomination_Other_Religions_beta, exclude = NULL)

table(issp_2007$religious_denomination_Protestant, exclude = NULL)
issp_2007$religious_denomination_Protestant_avg <- mean(issp_2007$religious_denomination_Protestant, na.rm = T)
issp_2007$religious_denomination_Protestant_beta <- (issp_2007$religious_denomination_Protestant-issp_2007$religious_denomination_Protestant_avg)
table(issp_2007$religious_denomination_Protestant_beta, exclude = NULL)

table(issp_2007$religious_denomination_Roman_Catholic, exclude = NULL)
issp_2007$religious_denomination_Roman_Catholic_avg <- mean(issp_2007$religious_denomination_Roman_Catholic, na.rm = T)
issp_2007$religious_denomination_Roman_Catholic_beta <- (issp_2007$religious_denomination_Roman_Catholic-issp_2007$religious_denomination_Roman_Catholic_avg)
table(issp_2007$religious_denomination_Roman_Catholic_beta, exclude = NULL)

#### Manuscript, Table 1. Descriptive Statistics ####

table1::table1(~ PARTY_LR + religiosity +
                 chronotype_interval +
         urban_rural_area +
         factor(sex) + age + degree + income + political_interest | country, data=issp_2007, digits=3)

cor.test(issp_2007$chronotype_interval, issp_2007$religiosity)
cor.test(subset(issp_2007, subset = country == "Finland")$PARTY_LR, subset(issp_2007, subset = country == "Finland")$religiosity)
cor.test(subset(issp_2007, subset = country == "Greece")$PARTY_LR, subset(issp_2007, subset = country == "Greece")$religiosity)
cor.test(subset(issp_2007, subset = country == "Ireland")$PARTY_LR, subset(issp_2007, subset = country == "Ireland")$religiosity)
cor.test(subset(issp_2007, subset = country == "Mexico")$PARTY_LR, subset(issp_2007, subset = country == "Mexico")$religiosity)
cor.test(subset(issp_2007, subset = country == "the Netherlands")$PARTY_LR, subset(issp_2007, subset = country == "the Netherlands")$religiosity)
cor.test(subset(issp_2007, subset = country == "New Zealand")$PARTY_LR, subset(issp_2007, subset = country == "New Zealand")$religiosity)
cor.test(subset(issp_2007, subset = country == "the Philippines")$PARTY_LR, subset(issp_2007, subset = country == "the Philippines")$religiosity)
cor.test(subset(issp_2007, subset = country == "Russia")$PARTY_LR, subset(issp_2007, subset = country == "Russia")$religiosity)
cor.test(subset(issp_2007, subset = country == "South Korea")$PARTY_LR, subset(issp_2007, subset = country == "South Korea")$religiosity)
cor.test(subset(issp_2007, subset = country == "Switzerland")$PARTY_LR, subset(issp_2007, subset = country == "Switzerland")$religiosity)

table(issp_2007$PARTY_LR, exclude = NULL)
issp_2007$`Left-Right Ideological Placement` <- NA
issp_2007$`Left-Right Ideological Placement`[is.na(issp_2007$PARTY_LR)] <- "Missing"
issp_2007$`Left-Right Ideological Placement` <- ifelse(is.na(issp_2007$`Left-Right Ideological Placement`), "Not Missing", issp_2007$`Left-Right Ideological Placement`)
table(issp_2007$`Left-Right Ideological Placement`, exclude = NULL)

#### Online Appendices, Figure 1. Distribution of Mid-Point Timing of Sleep (o’clock) across Countries and Sample with and without Missing Left-Right Ideological Placement ####

FI.LR.CHRO <- subset(issp_2007, subset = country == "Finland", select = c("ORIGINAL_MID_POINT", "Left-Right Ideological Placement", "chronotype_interval"))
table(FI.LR.CHRO$`Left-Right Ideological Placement`, exclude = NULL)
table(FI.LR.CHRO$ORIGINAL_MID_POINT, exclude = NULL)

FI.LR.CHRO$`Left-Right Ideological Placement` <- factor(FI.LR.CHRO$`Left-Right Ideological Placement`,
                                                        levels = c("Missing", "Not Missing"),
                                                        labels = c("Ideology Missing (45.53%)", "Ideology Not Missing (54.47%)"))

psych::describeBy(FI.LR.CHRO$ORIGINAL_MID_POINT, FI.LR.CHRO$`Left-Right Ideological Placement`)
t.test(FI.LR.CHRO$chronotype_interval ~ factor(FI.LR.CHRO$`Left-Right Ideological Placement`))
psych::describeBy(FI.LR.CHRO$chronotype_interval, FI.LR.CHRO$`Left-Right Ideological Placement`)

PLOT.FI <- ggplot(FI.LR.CHRO, aes(x = ORIGINAL_MID_POINT, y = (..count..)/sum(..count..)*100)) +
  geom_histogram(color="black", alpha=0.6, position = 'identity', bins = 50) +
  scale_x_continuous() +
  theme_classic() +
  theme(axis.text.x=element_text(colour="black", size = 12, angle = 90, vjust = 1, hjust=1), axis.text.y=element_text(colour="black", size = 12)) +
  theme(axis.title=element_text(size=14, colour="black", face="bold"), legend.text=element_text(size=12), legend.title=element_text(size=12)) +
  labs(    x = "Mid-Point Timing of Sleep", 
           y = "Percentage (%) \n in the Sample from \nFinland (N=1265)",
           title = "") +
  theme(strip.text.x = element_text(size = 12, colour = "black")) +
  ggpubr::grids(linetype = "dashed") +
  scale_x_continuous(breaks = c(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 23.99), limits = c(0, 23.99),
                     labels = c("0:00", "", "", "3:00", "", "", "6:00", "", "", "9:00", "", "", "12:00", "", "", "15:00", "", "", "18:00", "", "", "21:00", "", "", "23:59")) +
  facet_wrap(.~`Left-Right Ideological Placement`) +
  theme(strip.text.x = element_text(size = 12, margin = margin (5, 5, 5, 5))) +
  scale_y_continuous(breaks = c(0, 4, 8, 12, 16, 20), limits = c(0, 20))

PLOT.FI

GR.LR.CHRO <- subset(issp_2007, subset = country == "Greece", select = c("ORIGINAL_MID_POINT", "Left-Right Ideological Placement", "chronotype_interval"))
table(GR.LR.CHRO$`Left-Right Ideological Placement`, exclude = NULL)
table(GR.LR.CHRO$ORIGINAL_MID_POINT, exclude = NULL)

GR.LR.CHRO$`Left-Right Ideological Placement` <- factor(GR.LR.CHRO$`Left-Right Ideological Placement`,
                                                        levels = c("Missing", "Not Missing"),
                                                        labels = c("Ideology Missing (10.67%)", "Ideology Not Missing (89.33%)"))

psych::describeBy(GR.LR.CHRO$ORIGINAL_MID_POINT, GR.LR.CHRO$`Left-Right Ideological Placement`)
t.test(GR.LR.CHRO$chronotype_interval ~ factor(GR.LR.CHRO$`Left-Right Ideological Placement`))

PLOT.GR <- ggplot(GR.LR.CHRO, aes(x = ORIGINAL_MID_POINT, y = (..count..)/sum(..count..)*100)) +
  geom_histogram(color="black", alpha=0.6, position = 'identity', bins = 50) +
  scale_x_continuous() +
  theme_classic() +
  theme(axis.text.x=element_text(colour="black", size = 12, angle = 90, vjust = 1, hjust=1), axis.text.y=element_text(colour="black", size = 12)) +
  theme(axis.title=element_text(size=14, colour="black", face="bold"), legend.text=element_text(size=12), legend.title=element_text(size=12)) +
  labs(    x = "Mid-Point Timing of Sleep", 
           y = "Percentage (%) \n in the Sample from \nGreece (N=750)",
           title = "") +
  theme(strip.text.x = element_text(size = 12, colour = "black")) +
  ggpubr::grids(linetype = "dashed") +
  scale_x_continuous(breaks = c(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 23.99), limits = c(0, 23.99),
                     labels = c("0:00", "", "", "3:00", "", "", "6:00", "", "", "9:00", "", "", "12:00", "", "", "15:00", "", "", "18:00", "", "", "21:00", "", "", "23:59")) +
  facet_wrap(.~`Left-Right Ideological Placement`) +
  theme(strip.text.x = element_text(size = 12, margin = margin (5, 5, 5, 5))) +
  scale_y_continuous(breaks = c(0, 4, 8, 12, 16, 20), limits = c(0, 20))

PLOT.GR

IE.LR.CHRO <- subset(issp_2007, subset = country == "Ireland", select = c("ORIGINAL_MID_POINT", "Left-Right Ideological Placement", "chronotype_interval"))
table(IE.LR.CHRO$`Left-Right Ideological Placement`, exclude = NULL)
table(IE.LR.CHRO$ORIGINAL_MID_POINT, exclude = NULL)

IE.LR.CHRO$`Left-Right Ideological Placement` <- factor(IE.LR.CHRO$`Left-Right Ideological Placement`,
                                                        levels = c("Missing", "Not Missing"),
                                                        labels = c("Ideology Missing (41.42%)", "Ideology Not Missing (58.58%)"))

psych::describeBy(IE.LR.CHRO$ORIGINAL_MID_POINT, IE.LR.CHRO$`Left-Right Ideological Placement`)
t.test(IE.LR.CHRO$chronotype_interval ~ factor(IE.LR.CHRO$`Left-Right Ideological Placement`))

PLOT.IE <- ggplot(IE.LR.CHRO, aes(x = ORIGINAL_MID_POINT, y = (..count..)/sum(..count..)*100)) +
  geom_histogram(color="black", alpha=0.6, position = 'identity', bins = 50) +
  scale_x_continuous() +
  theme_classic() +
  theme(axis.text.x=element_text(colour="black", size = 12, angle = 90, vjust = 1, hjust=1), axis.text.y=element_text(colour="black", size = 12)) +
  theme(axis.title=element_text(size=14, colour="black", face="bold"), legend.text=element_text(size=12), legend.title=element_text(size=12)) +
  labs(    x = "Mid-Point Timing of Sleep", 
           y = "Percentage (%) \n in the Sample from \nIreland (N=1,975)",
           title = "") +
  theme(strip.text.x = element_text(size = 12, colour = "black")) +
  ggpubr::grids(linetype = "dashed") +
  scale_x_continuous(breaks = c(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 23.99), limits = c(0, 23.99),
                     labels = c("0:00", "", "", "3:00", "", "", "6:00", "", "", "9:00", "", "", "12:00", "", "", "15:00", "", "", "18:00", "", "", "21:00", "", "", "23:59")) +
  facet_wrap(.~`Left-Right Ideological Placement`) +
  theme(strip.text.x = element_text(size = 12, margin = margin (5, 5, 5, 5))) +
  scale_y_continuous(breaks = c(0, 4, 8, 12, 16, 20), limits = c(0, 20))

PLOT.IE

MX.LR.CHRO <- subset(issp_2007, subset = country == "Mexico", select = c("ORIGINAL_MID_POINT", "Left-Right Ideological Placement", "chronotype_interval"))
table(MX.LR.CHRO$`Left-Right Ideological Placement`, exclude = NULL)
table(MX.LR.CHRO$ORIGINAL_MID_POINT, exclude = NULL)

MX.LR.CHRO$`Left-Right Ideological Placement` <- factor(MX.LR.CHRO$`Left-Right Ideological Placement`,
                                                        levels = c("Missing", "Not Missing"),
                                                        labels = c("Ideology Missing (73.67%)", "Ideology Not Missing (26.33%)"))

psych::describeBy(MX.LR.CHRO$ORIGINAL_MID_POINT, MX.LR.CHRO$`Left-Right Ideological Placement`)
t.test(MX.LR.CHRO$chronotype_interval ~ factor(MX.LR.CHRO$`Left-Right Ideological Placement`))

PLOT.MX <- ggplot(MX.LR.CHRO, aes(x = ORIGINAL_MID_POINT, y = (..count..)/sum(..count..)*100)) +
  geom_histogram(color="black", alpha=0.6, position = 'identity', bins = 50) +
  scale_x_continuous() +
  theme_classic() +
  theme(axis.text.x=element_text(colour="black", size = 12, angle = 90, vjust = 1, hjust=1), axis.text.y=element_text(colour="black", size = 12)) +
  theme(axis.title=element_text(size=14, colour="black", face="bold"), legend.text=element_text(size=12), legend.title=element_text(size=12)) +
  labs(    x = "Mid-Point Timing of Sleep", 
           y = "Percentage (%) \n in the Sample from \nMexico (N=1,527)",
           title = "") +
  theme(strip.text.x = element_text(size = 12, colour = "black")) +
  ggpubr::grids(linetype = "dashed") +
  scale_x_continuous(breaks = c(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 23.99), limits = c(0, 23.99),
                     labels = c("0:00", "", "", "3:00", "", "", "6:00", "", "", "9:00", "", "", "12:00", "", "", "15:00", "", "", "18:00", "", "", "21:00", "", "", "23:59")) +
  facet_wrap(.~`Left-Right Ideological Placement`) +
  theme(strip.text.x = element_text(size = 12, margin = margin (5, 5, 5, 5))) +
  scale_y_continuous(breaks = c(0, 4, 8, 12, 16, 20), limits = c(0, 20))

PLOT.MX

NL.LR.CHRO <- subset(issp_2007, subset = country == "the Netherlands", select = c("ORIGINAL_MID_POINT", "Left-Right Ideological Placement", "chronotype_interval"))
table(NL.LR.CHRO$`Left-Right Ideological Placement`, exclude = NULL)
table(NL.LR.CHRO$ORIGINAL_MID_POINT, exclude = NULL)

NL.LR.CHRO$`Left-Right Ideological Placement` <- factor(NL.LR.CHRO$`Left-Right Ideological Placement`,
                                                        levels = c("Missing", "Not Missing"),
                                                        labels = c("Ideology Missing (23.28%)", "Ideology Not Missing (76.72%)"))

psych::describeBy(NL.LR.CHRO$ORIGINAL_MID_POINT, NL.LR.CHRO$`Left-Right Ideological Placement`)
t.test(NL.LR.CHRO$chronotype_interval ~ factor(NL.LR.CHRO$`Left-Right Ideological Placement`))

PLOT.NL <- ggplot(NL.LR.CHRO, aes(x = ORIGINAL_MID_POINT, y = (..count..)/sum(..count..)*100)) +
  geom_histogram(color="black", alpha=0.6, position = 'identity', bins = 50) +
  scale_x_continuous() +
  theme_classic() +
  theme(axis.text.x=element_text(colour="black", size = 12, angle = 90, vjust = 1, hjust=1), axis.text.y=element_text(colour="black", size = 12)) +
  theme(axis.title=element_text(size=14, colour="black", face="bold"), legend.text=element_text(size=12), legend.title=element_text(size=12)) +
  labs(    x = "Mid-Point Timing of Sleep", 
           y = "Percentage (%) \n in the Sample from \nthe Netherlands (N=859)",
           title = "") +
  theme(strip.text.x = element_text(size = 12, colour = "black")) +
  ggpubr::grids(linetype = "dashed") +
  scale_x_continuous(breaks = c(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 23.99), limits = c(0, 23.99),
                     labels = c("0:00", "", "", "3:00", "", "", "6:00", "", "", "9:00", "", "", "12:00", "", "", "15:00", "", "", "18:00", "", "", "21:00", "", "", "23:59")) +
  facet_wrap(.~`Left-Right Ideological Placement`) +
  theme(strip.text.x = element_text(size = 12, margin = margin (5, 5, 5, 5))) +
  scale_y_continuous(breaks = c(0, 4, 8, 12, 16, 20), limits = c(0, 20))

PLOT.NL

NZ.LR.CHRO <- subset(issp_2007, subset = country == "New Zealand", select = c("ORIGINAL_MID_POINT", "Left-Right Ideological Placement", "chronotype_interval"))
table(NZ.LR.CHRO$`Left-Right Ideological Placement`, exclude = NULL)
table(NZ.LR.CHRO$ORIGINAL_MID_POINT, exclude = NULL)

NZ.LR.CHRO$`Left-Right Ideological Placement` <- factor(NZ.LR.CHRO$`Left-Right Ideological Placement`,
                                                        levels = c("Missing", "Not Missing"),
                                                        labels = c("Ideology Missing (46.2%)", "Ideology Not Missing (53.8%)"))

psych::describeBy(NZ.LR.CHRO$ORIGINAL_MID_POINT, NZ.LR.CHRO$`Left-Right Ideological Placement`)
t.test(NZ.LR.CHRO$chronotype_interval ~ factor(NZ.LR.CHRO$`Left-Right Ideological Placement`))

PLOT.NZ <- ggplot(NZ.LR.CHRO, aes(x = ORIGINAL_MID_POINT, y = (..count..)/sum(..count..)*100)) +
  geom_histogram(color="black", alpha=0.6, position = 'identity', bins = 50) +
  scale_x_continuous() +
  theme_classic() +
  theme(axis.text.x=element_text(colour="black", size = 12, angle = 90, vjust = 1, hjust=1), axis.text.y=element_text(colour="black", size = 12)) +
  theme(axis.title=element_text(size=14, colour="black", face="bold"), legend.text=element_text(size=12), legend.title=element_text(size=12)) +
  labs(    x = "Mid-Point Timing of Sleep", 
           y = "Percentage (%) \n in the Sample from \nNew Zealand (N=948)",
           title = "") +
  theme(strip.text.x = element_text(size = 12, colour = "black")) +
  ggpubr::grids(linetype = "dashed") +
  scale_x_continuous(breaks = c(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 23.99), limits = c(0, 23.99),
                     labels = c("0:00", "", "", "3:00", "", "", "6:00", "", "", "9:00", "", "", "12:00", "", "", "15:00", "", "", "18:00", "", "", "21:00", "", "", "23:59")) +
  facet_wrap(.~`Left-Right Ideological Placement`) +
  theme(strip.text.x = element_text(size = 12, margin = margin (5, 5, 5, 5))) +
  scale_y_continuous(breaks = c(0, 4, 8, 12, 16, 20), limits = c(0, 20))

PLOT.NZ

PH.LR.CHRO <- subset(issp_2007, subset = country == "the Philippines", select = c("ORIGINAL_MID_POINT", "Left-Right Ideological Placement", "chronotype_interval"))
table(PH.LR.CHRO$`Left-Right Ideological Placement`, exclude = NULL)
table(PH.LR.CHRO$ORIGINAL_MID_POINT, exclude = NULL)

PH.LR.CHRO$`Left-Right Ideological Placement` <- factor(PH.LR.CHRO$`Left-Right Ideological Placement`,
                                                        levels = c("Missing", "Not Missing"),
                                                        labels = c("Ideology Missing (61.36%)", "Ideology Not Missing (38.64%)"))

psych::describeBy(PH.LR.CHRO$ORIGINAL_MID_POINT, PH.LR.CHRO$`Left-Right Ideological Placement`)
t.test(PH.LR.CHRO$chronotype_interval ~ factor(PH.LR.CHRO$`Left-Right Ideological Placement`))
psych::describeBy(PH.LR.CHRO$chronotype_interval, PH.LR.CHRO$`Left-Right Ideological Placement`)

PLOT.PH <- ggplot(PH.LR.CHRO, aes(x = ORIGINAL_MID_POINT, y = (..count..)/sum(..count..)*100)) +
  geom_histogram(color="black", alpha=0.6, position = 'identity', bins = 50) +
  scale_x_continuous() +
  theme_classic() +
  theme(axis.text.x=element_text(colour="black", size = 12, angle = 90, vjust = 1, hjust=1), axis.text.y=element_text(colour="black", size = 12)) +
  theme(axis.title=element_text(size=14, colour="black", face="bold"), legend.text=element_text(size=12), legend.title=element_text(size=12)) +
  labs(    x = "Mid-Point Timing of Sleep", 
           y = "Percentage (%) \n in the Sample from \nthe Philippines (N=1,092)",
           title = "") +
  theme(strip.text.x = element_text(size = 12, colour = "black")) +
  ggpubr::grids(linetype = "dashed") +
  scale_x_continuous(breaks = c(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 23.99), limits = c(0, 23.99),
                     labels = c("0:00", "", "", "3:00", "", "", "6:00", "", "", "9:00", "", "", "12:00", "", "", "15:00", "", "", "18:00", "", "", "21:00", "", "", "23:59")) +
  facet_wrap(.~`Left-Right Ideological Placement`) +
  theme(strip.text.x = element_text(size = 12, margin = margin (5, 5, 5, 5))) +
  scale_y_continuous(breaks = c(0, 4, 8, 12, 16, 20), limits = c(0, 20))

PLOT.PH

RU.LR.CHRO <- subset(issp_2007, subset = country == "Russia", select = c("ORIGINAL_MID_POINT", "Left-Right Ideological Placement", "chronotype_interval"))
table(RU.LR.CHRO$`Left-Right Ideological Placement`, exclude = NULL)
table(RU.LR.CHRO$ORIGINAL_MID_POINT, exclude = NULL)

RU.LR.CHRO$`Left-Right Ideological Placement` <- factor(RU.LR.CHRO$`Left-Right Ideological Placement`,
                                                        levels = c("Missing", "Not Missing"),
                                                        labels = c("Ideology Missing (81.34%)", "Ideology Not Missing (18.66%)"))

psych::describeBy(RU.LR.CHRO$ORIGINAL_MID_POINT, RU.LR.CHRO$`Left-Right Ideological Placement`)
t.test(RU.LR.CHRO$chronotype_interval ~ factor(RU.LR.CHRO$`Left-Right Ideological Placement`))
psych::describeBy(RU.LR.CHRO$chronotype_interval, RU.LR.CHRO$`Left-Right Ideological Placement`)

PLOT.RU <- ggplot(RU.LR.CHRO, aes(x = ORIGINAL_MID_POINT, y = (..count..)/sum(..count..)*100)) +
  geom_histogram(color="black", alpha=0.6, position = 'identity', bins = 50) +
  scale_x_continuous() +
  theme_classic() +
  theme(axis.text.x=element_text(colour="black", size = 12, angle = 90, vjust = 1, hjust=1), axis.text.y=element_text(colour="black", size = 12)) +
  theme(axis.title=element_text(size=14, colour="black", face="bold"), legend.text=element_text(size=12), legend.title=element_text(size=12)) +
  labs(    x = "Mid-Point Timing of Sleep", 
           y = "Percentage (%) \n in the Sample from \nRussia (N=1,811)",
           title = "") +
  theme(strip.text.x = element_text(size = 12, colour = "black")) +
  ggpubr::grids(linetype = "dashed") +
  scale_x_continuous(breaks = c(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 23.99), limits = c(0, 23.99),
                     labels = c("0:00", "", "", "3:00", "", "", "6:00", "", "", "9:00", "", "", "12:00", "", "", "15:00", "", "", "18:00", "", "", "21:00", "", "", "23:59")) +
  facet_wrap(.~`Left-Right Ideological Placement`) +
  theme(strip.text.x = element_text(size = 12, margin = margin (5, 5, 5, 5))) +
  scale_y_continuous(breaks = c(0, 4, 8, 12, 16, 20), limits = c(0, 20))

PLOT.RU

SK.LR.CHRO <- subset(issp_2007, subset = country == "South Korea", select = c("ORIGINAL_MID_POINT", "Left-Right Ideological Placement", "chronotype_interval"))
table(SK.LR.CHRO$`Left-Right Ideological Placement`, exclude = NULL)
table(SK.LR.CHRO$ORIGINAL_MID_POINT, exclude = NULL)

SK.LR.CHRO$`Left-Right Ideological Placement` <- factor(SK.LR.CHRO$`Left-Right Ideological Placement`,
                                                        levels = c("Missing", "Not Missing"),
                                                        labels = c("Ideology Missing (3.91%)", "Ideology Not Missing (96.09%)"))

psych::describeBy(SK.LR.CHRO$ORIGINAL_MID_POINT, SK.LR.CHRO$`Left-Right Ideological Placement`)
t.test(SK.LR.CHRO$chronotype_interval ~ factor(SK.LR.CHRO$`Left-Right Ideological Placement`))
psych::describeBy(SK.LR.CHRO$chronotype_interval, SK.LR.CHRO$`Left-Right Ideological Placement`)

PLOT.SK <- ggplot(SK.LR.CHRO, aes(x = ORIGINAL_MID_POINT, y = (..count..)/sum(..count..)*100)) +
  geom_histogram(color="black", alpha=0.6, position = 'identity', bins = 50) +
  scale_x_continuous() +
  theme_classic() +
  theme(axis.text.x=element_text(colour="black", size = 12, angle = 90, vjust = 1, hjust=1), axis.text.y=element_text(colour="black", size = 12)) +
  theme(axis.title=element_text(size=14, colour="black", face="bold"), legend.text=element_text(size=12), legend.title=element_text(size=12)) +
  labs(    x = "Mid-Point Timing of Sleep", 
           y = "Percentage (%) \n in the Sample from \nSouth Korea (N=1,329)",
           title = "") +
  theme(strip.text.x = element_text(size = 12, colour = "black")) +
  ggpubr::grids(linetype = "dashed") +
  scale_x_continuous(breaks = c(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 23.99), limits = c(0, 23.99),
                     labels = c("0:00", "", "", "3:00", "", "", "6:00", "", "", "9:00", "", "", "12:00", "", "", "15:00", "", "", "18:00", "", "", "21:00", "", "", "23:59")) +
  facet_wrap(.~`Left-Right Ideological Placement`) +
  theme(strip.text.x = element_text(size = 12, margin = margin (5, 5, 5, 5))) +
  scale_y_continuous(breaks = c(0, 4, 8, 12, 16, 20), limits = c(0, 20))

PLOT.SK

CH.LR.CHRO <- subset(issp_2007, subset = country == "Switzerland", select = c("ORIGINAL_MID_POINT", "Left-Right Ideological Placement", "chronotype_interval"))
table(CH.LR.CHRO$`Left-Right Ideological Placement`, exclude = NULL)
table(CH.LR.CHRO$ORIGINAL_MID_POINT, exclude = NULL)

CH.LR.CHRO$`Left-Right Ideological Placement` <- factor(CH.LR.CHRO$`Left-Right Ideological Placement`,
                                                        levels = c("Missing", "Not Missing"),
                                                        labels = c("Ideology Missing (49.26%)", "Ideology Not Missing (50.74%)"))

psych::describeBy(CH.LR.CHRO$ORIGINAL_MID_POINT, CH.LR.CHRO$`Left-Right Ideological Placement`)
t.test(CH.LR.CHRO$chronotype_interval ~ factor(CH.LR.CHRO$`Left-Right Ideological Placement`))

PLOT.CH <- ggplot(CH.LR.CHRO, aes(x = ORIGINAL_MID_POINT, y = (..count..)/sum(..count..)*100)) +
  geom_histogram(color="black", alpha=0.6, position = 'identity', bins = 50) +
  scale_x_continuous() +
  theme_classic() +
  theme(axis.text.x=element_text(colour="black", size = 12, angle = 90, vjust = 1, hjust=1), axis.text.y=element_text(colour="black", size = 12)) +
  theme(axis.title=element_text(size=14, colour="black", face="bold"), legend.text=element_text(size=12), legend.title=element_text(size=12)) +
  labs(    x = "Mid-Point Timing of Sleep", 
           y = "Percentage (%) \n in the Sample from \nSwitzerland (N=879)",
           title = "") +
  theme(strip.text.x = element_text(size = 12, colour = "black")) +
  ggpubr::grids(linetype = "dashed") +
  scale_x_continuous(breaks = c(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 23.99), limits = c(0, 23.99),
                     labels = c("0:00", "", "", "3:00", "", "", "6:00", "", "", "9:00", "", "", "12:00", "", "", "15:00", "", "", "18:00", "", "", "21:00", "", "", "23:59")) +
  facet_wrap(.~`Left-Right Ideological Placement`) +
  theme(strip.text.x = element_text(size = 12, margin = margin (5, 5, 5, 5))) +
  scale_y_continuous(breaks = c(0, 4, 8, 12, 16, 20), limits = c(0, 20))

PLOT.CH

library(cowplot)

missing_ideology_chronotype_plot <- plot_grid(PLOT.FI,
                                              PLOT.GR,
                                              PLOT.IE,
                                              PLOT.MX,
                                              PLOT.NL,
                                              PLOT.NZ,
                                              PLOT.PH,
                                              PLOT.RU,
                                              PLOT.SK,
                                              PLOT.CH,
                                              nrow = 5, ncol = 2, align = 'hv', greedy = T)

ggsave(filename = "missing_ideology_chronotype_plot.jpg", plot = missing_ideology_chronotype_plot, width = 16, height = 12, dpi = 1000)

#### Models ####

#### Left-Right Ideological Placement ####

issp_2007_left_right_model1 <- lmer(PARTY_LR_beta ~ chronotype_interval_beta + (1 + chronotype_interval_beta | country), 
                                    data =  issp_2007)
summary(issp_2007_left_right_model1)
tab_model(issp_2007_left_right_model1, show.ci = 0.95)
round(variancePartition::calcVarPart(issp_2007_left_right_model1)*100,2)
lmerTest::ranova(issp_2007_left_right_model1)

issp_2007_left_right_model2 <- lmer(PARTY_LR_beta ~ chronotype_interval_beta + urban_rural_area_beta + (1 + chronotype_interval_beta | country), 
                                    data =  issp_2007)
summary(issp_2007_left_right_model2)
tab_model(issp_2007_left_right_model2, show.ci = 0.95)
round(variancePartition::calcVarPart(issp_2007_left_right_model2)*100,2)
lmerTest::ranova(issp_2007_left_right_model2)

issp_2007_left_right_model3 <- lmer(PARTY_LR_beta ~ chronotype_interval_beta + urban_rural_area_beta +
                                      sex_beta + age_beta + degree_beta + income_beta + religiosity_beta + political_interest_beta + (1 + chronotype_interval_beta | country), 
                                    data =  issp_2007)
summary(issp_2007_left_right_model3)
tab_model(issp_2007_left_right_model3, show.ci = 0.95)
round(variancePartition::calcVarPart(issp_2007_left_right_model3)*100,2)
lmerTest::ranova(issp_2007_left_right_model3)

#### Manuscript, Table 2A: Multilevel linear modeling of left-right ideological placement ####


stargazer(issp_2007_left_right_model1, issp_2007_left_right_model2, issp_2007_left_right_model3,
          type = "html", title=" ", digits=2, out="Tables/models_issp_2007_left_right.htm",
          model.numbers = F,
          column.labels = c("Model 1", "Model 2", "Model 3"),
          covariate.labels = c("Chronotype", 
                               "Urban-Rural Area of Residence", "Sex: Male (Base: Female)",
                               "Age", "Education", "Income",
                               "Religious Attendance", "Level on Interest in Politics",
                               "Intercept"))

tab_model(issp_2007_left_right_model1, show.ci = 0.95)
tab_model(issp_2007_left_right_model2, show.ci = 0.95)
tab_model(issp_2007_left_right_model3, show.ci = 0.95)

#### Preparation for Manuscript, Table 2B: Predicted effects of chronotype on left-right ideological placement for each country ####

coef(issp_2007_left_right_model1)
arm::se.coef(issp_2007_left_right_model1)

coef(issp_2007_left_right_model1)$country
coef_issp_2007_left_right_model1 <- data.frame(coef(issp_2007_left_right_model1)$country)
coef_issp_2007_left_right_model1 <- subset(coef_issp_2007_left_right_model1, select = c("chronotype_interval_beta"))
coef_issp_2007_left_right_model1$country <- row.names(coef_issp_2007_left_right_model1)
names(coef_issp_2007_left_right_model1)[names(coef_issp_2007_left_right_model1)=="chronotype_interval_beta"] <- "coef_chronotype_interval_beta"


se_coef_issp_2007_left_right_model1 <- data.frame(arm::se.coef(issp_2007_left_right_model1)$country)
se_coef_issp_2007_left_right_model1 <- subset(se_coef_issp_2007_left_right_model1, select = c("chronotype_interval_beta"))
se_coef_issp_2007_left_right_model1$country <- row.names(se_coef_issp_2007_left_right_model1)
names(se_coef_issp_2007_left_right_model1)[names(se_coef_issp_2007_left_right_model1)=="chronotype_interval_beta"] <- "se_chronotype_interval_beta"

coef_se_issp_2007_left_right_model1 <- merge(coef_issp_2007_left_right_model1, se_coef_issp_2007_left_right_model1, by="country")

coef_se_issp_2007_left_right_model1$chrono.cl.90 <- coef_se_issp_2007_left_right_model1$coef_chronotype_interval_beta - 1.645*coef_se_issp_2007_left_right_model1$se_chronotype_interval_beta
coef_se_issp_2007_left_right_model1$chrono.cu.90 <- coef_se_issp_2007_left_right_model1$coef_chronotype_interval_beta + 1.645*coef_se_issp_2007_left_right_model1$se_chronotype_interval_beta
coef_se_issp_2007_left_right_model1$chrono.cl.95 <- coef_se_issp_2007_left_right_model1$coef_chronotype_interval_beta - 1.96*coef_se_issp_2007_left_right_model1$se_chronotype_interval_beta
coef_se_issp_2007_left_right_model1$chrono.cu.95 <- coef_se_issp_2007_left_right_model1$coef_chronotype_interval_beta + 1.96*coef_se_issp_2007_left_right_model1$se_chronotype_interval_beta
coef_se_issp_2007_left_right_model1$chrono.cl.99 <- coef_se_issp_2007_left_right_model1$coef_chronotype_interval_beta - 2.576*coef_se_issp_2007_left_right_model1$se_chronotype_interval_beta
coef_se_issp_2007_left_right_model1$chrono.cu.99 <- coef_se_issp_2007_left_right_model1$coef_chronotype_interval_beta + 2.576*coef_se_issp_2007_left_right_model1$se_chronotype_interval_beta

View(coef_se_issp_2007_left_right_model1)

coef(issp_2007_left_right_model2)
arm::se.coef(issp_2007_left_right_model2)

coef(issp_2007_left_right_model2)$country
coef_issp_2007_left_right_model2 <- data.frame(coef(issp_2007_left_right_model2)$country)
coef_issp_2007_left_right_model2 <- subset(coef_issp_2007_left_right_model2, select = c("chronotype_interval_beta"))
coef_issp_2007_left_right_model2$country <- row.names(coef_issp_2007_left_right_model2)
names(coef_issp_2007_left_right_model2)[names(coef_issp_2007_left_right_model2)=="chronotype_interval_beta"] <- "coef_chronotype_interval_beta"


se_coef_issp_2007_left_right_model2 <- data.frame(arm::se.coef(issp_2007_left_right_model2)$country)
se_coef_issp_2007_left_right_model2 <- subset(se_coef_issp_2007_left_right_model2, select = c("chronotype_interval_beta"))
se_coef_issp_2007_left_right_model2$country <- row.names(se_coef_issp_2007_left_right_model2)
names(se_coef_issp_2007_left_right_model2)[names(se_coef_issp_2007_left_right_model2)=="chronotype_interval_beta"] <- "se_chronotype_interval_beta"

coef_se_issp_2007_left_right_model2 <- merge(coef_issp_2007_left_right_model2, se_coef_issp_2007_left_right_model2, by="country")

coef_se_issp_2007_left_right_model2$chrono.cl.90 <- coef_se_issp_2007_left_right_model2$coef_chronotype_interval_beta - 1.645*coef_se_issp_2007_left_right_model2$se_chronotype_interval_beta
coef_se_issp_2007_left_right_model2$chrono.cu.90 <- coef_se_issp_2007_left_right_model2$coef_chronotype_interval_beta + 1.645*coef_se_issp_2007_left_right_model2$se_chronotype_interval_beta
coef_se_issp_2007_left_right_model2$chrono.cl.95 <- coef_se_issp_2007_left_right_model2$coef_chronotype_interval_beta - 1.96*coef_se_issp_2007_left_right_model2$se_chronotype_interval_beta
coef_se_issp_2007_left_right_model2$chrono.cu.95 <- coef_se_issp_2007_left_right_model2$coef_chronotype_interval_beta + 1.96*coef_se_issp_2007_left_right_model2$se_chronotype_interval_beta
coef_se_issp_2007_left_right_model2$chrono.cl.99 <- coef_se_issp_2007_left_right_model2$coef_chronotype_interval_beta - 2.576*coef_se_issp_2007_left_right_model2$se_chronotype_interval_beta
coef_se_issp_2007_left_right_model2$chrono.cu.99 <- coef_se_issp_2007_left_right_model2$coef_chronotype_interval_beta + 2.576*coef_se_issp_2007_left_right_model2$se_chronotype_interval_beta

View(coef_se_issp_2007_left_right_model2)

coef(issp_2007_left_right_model3)
arm::se.coef(issp_2007_left_right_model3)

coef(issp_2007_left_right_model3)$country
coef_issp_2007_left_right_model3 <- data.frame(coef(issp_2007_left_right_model3)$country)
coef_issp_2007_left_right_model3 <- subset(coef_issp_2007_left_right_model3, select = c("chronotype_interval_beta"))
coef_issp_2007_left_right_model3$country <- row.names(coef_issp_2007_left_right_model3)
names(coef_issp_2007_left_right_model3)[names(coef_issp_2007_left_right_model3)=="chronotype_interval_beta"] <- "coef_chronotype_interval_beta"


se_coef_issp_2007_left_right_model3 <- data.frame(arm::se.coef(issp_2007_left_right_model3)$country)
se_coef_issp_2007_left_right_model3 <- subset(se_coef_issp_2007_left_right_model3, select = c("chronotype_interval_beta"))
se_coef_issp_2007_left_right_model3$country <- row.names(se_coef_issp_2007_left_right_model3)
names(se_coef_issp_2007_left_right_model3)[names(se_coef_issp_2007_left_right_model3)=="chronotype_interval_beta"] <- "se_chronotype_interval_beta"

coef_se_issp_2007_left_right_model3 <- merge(coef_issp_2007_left_right_model3, se_coef_issp_2007_left_right_model3, by="country")

coef_se_issp_2007_left_right_model3$chrono.cl.90 <- coef_se_issp_2007_left_right_model3$coef_chronotype_interval_beta - 1.645*coef_se_issp_2007_left_right_model3$se_chronotype_interval_beta
coef_se_issp_2007_left_right_model3$chrono.cu.90 <- coef_se_issp_2007_left_right_model3$coef_chronotype_interval_beta + 1.645*coef_se_issp_2007_left_right_model3$se_chronotype_interval_beta
coef_se_issp_2007_left_right_model3$chrono.cl.95 <- coef_se_issp_2007_left_right_model3$coef_chronotype_interval_beta - 1.96*coef_se_issp_2007_left_right_model3$se_chronotype_interval_beta
coef_se_issp_2007_left_right_model3$chrono.cu.95 <- coef_se_issp_2007_left_right_model3$coef_chronotype_interval_beta + 1.96*coef_se_issp_2007_left_right_model3$se_chronotype_interval_beta
coef_se_issp_2007_left_right_model3$chrono.cl.99 <- coef_se_issp_2007_left_right_model3$coef_chronotype_interval_beta - 2.576*coef_se_issp_2007_left_right_model3$se_chronotype_interval_beta
coef_se_issp_2007_left_right_model3$chrono.cu.99 <- coef_se_issp_2007_left_right_model3$coef_chronotype_interval_beta + 2.576*coef_se_issp_2007_left_right_model3$se_chronotype_interval_beta

View(coef_se_issp_2007_left_right_model3)

#### Preparation Online Appendices, Table 1: Random effects of chronotype on left-right ideological placement for each country ####

ranef(issp_2007_left_right_model1)
arm::se.ranef(issp_2007_left_right_model1)

ranef(issp_2007_left_right_model1)$country
ranef_issp_2007_left_right_model1 <- data.frame(ranef(issp_2007_left_right_model1)$country)
ranef_issp_2007_left_right_model1 <- subset(ranef_issp_2007_left_right_model1, select = c("chronotype_interval_beta"))
ranef_issp_2007_left_right_model1$country <- row.names(ranef_issp_2007_left_right_model1)
names(ranef_issp_2007_left_right_model1)[names(ranef_issp_2007_left_right_model1)=="chronotype_interval_beta"] <- "ranef_chronotype_interval_beta"


se_ranef_issp_2007_left_right_model1 <- data.frame(arm::se.ranef(issp_2007_left_right_model1)$country)
se_ranef_issp_2007_left_right_model1 <- subset(se_ranef_issp_2007_left_right_model1, select = c("chronotype_interval_beta"))
se_ranef_issp_2007_left_right_model1$country <- row.names(se_ranef_issp_2007_left_right_model1)
names(se_ranef_issp_2007_left_right_model1)[names(se_ranef_issp_2007_left_right_model1)=="chronotype_interval_beta"] <- "se_chronotype_interval_beta"

ranef_se_issp_2007_left_right_model1 <- merge(ranef_issp_2007_left_right_model1, se_ranef_issp_2007_left_right_model1, by="country")

ranef_se_issp_2007_left_right_model1$chrono.cl.90 <- ranef_se_issp_2007_left_right_model1$ranef_chronotype_interval_beta - 1.645*ranef_se_issp_2007_left_right_model1$se_chronotype_interval_beta
ranef_se_issp_2007_left_right_model1$chrono.cu.90 <- ranef_se_issp_2007_left_right_model1$ranef_chronotype_interval_beta + 1.645*ranef_se_issp_2007_left_right_model1$se_chronotype_interval_beta
ranef_se_issp_2007_left_right_model1$chrono.cl.95 <- ranef_se_issp_2007_left_right_model1$ranef_chronotype_interval_beta - 1.96*ranef_se_issp_2007_left_right_model1$se_chronotype_interval_beta
ranef_se_issp_2007_left_right_model1$chrono.cu.95 <- ranef_se_issp_2007_left_right_model1$ranef_chronotype_interval_beta + 1.96*ranef_se_issp_2007_left_right_model1$se_chronotype_interval_beta
ranef_se_issp_2007_left_right_model1$chrono.cl.99 <- ranef_se_issp_2007_left_right_model1$ranef_chronotype_interval_beta - 2.576*ranef_se_issp_2007_left_right_model1$se_chronotype_interval_beta
ranef_se_issp_2007_left_right_model1$chrono.cu.99 <- ranef_se_issp_2007_left_right_model1$ranef_chronotype_interval_beta + 2.576*ranef_se_issp_2007_left_right_model1$se_chronotype_interval_beta

View(ranef_se_issp_2007_left_right_model1)

ranef(issp_2007_left_right_model2)
arm::se.ranef(issp_2007_left_right_model2)

ranef(issp_2007_left_right_model2)$country
ranef_issp_2007_left_right_model2 <- data.frame(ranef(issp_2007_left_right_model2)$country)
ranef_issp_2007_left_right_model2 <- subset(ranef_issp_2007_left_right_model2, select = c("chronotype_interval_beta"))
ranef_issp_2007_left_right_model2$country <- row.names(ranef_issp_2007_left_right_model2)
names(ranef_issp_2007_left_right_model2)[names(ranef_issp_2007_left_right_model2)=="chronotype_interval_beta"] <- "ranef_chronotype_interval_beta"


se_ranef_issp_2007_left_right_model2 <- data.frame(arm::se.ranef(issp_2007_left_right_model2)$country)
se_ranef_issp_2007_left_right_model2 <- subset(se_ranef_issp_2007_left_right_model2, select = c("chronotype_interval_beta"))
se_ranef_issp_2007_left_right_model2$country <- row.names(se_ranef_issp_2007_left_right_model2)
names(se_ranef_issp_2007_left_right_model2)[names(se_ranef_issp_2007_left_right_model2)=="chronotype_interval_beta"] <- "se_chronotype_interval_beta"

ranef_se_issp_2007_left_right_model2 <- merge(ranef_issp_2007_left_right_model2, se_ranef_issp_2007_left_right_model2, by="country")

ranef_se_issp_2007_left_right_model2$chrono.cl.90 <- ranef_se_issp_2007_left_right_model2$ranef_chronotype_interval_beta - 1.645*ranef_se_issp_2007_left_right_model2$se_chronotype_interval_beta
ranef_se_issp_2007_left_right_model2$chrono.cu.90 <- ranef_se_issp_2007_left_right_model2$ranef_chronotype_interval_beta + 1.645*ranef_se_issp_2007_left_right_model2$se_chronotype_interval_beta
ranef_se_issp_2007_left_right_model2$chrono.cl.95 <- ranef_se_issp_2007_left_right_model2$ranef_chronotype_interval_beta - 1.96*ranef_se_issp_2007_left_right_model2$se_chronotype_interval_beta
ranef_se_issp_2007_left_right_model2$chrono.cu.95 <- ranef_se_issp_2007_left_right_model2$ranef_chronotype_interval_beta + 1.96*ranef_se_issp_2007_left_right_model2$se_chronotype_interval_beta
ranef_se_issp_2007_left_right_model2$chrono.cl.99 <- ranef_se_issp_2007_left_right_model2$ranef_chronotype_interval_beta - 2.576*ranef_se_issp_2007_left_right_model2$se_chronotype_interval_beta
ranef_se_issp_2007_left_right_model2$chrono.cu.99 <- ranef_se_issp_2007_left_right_model2$ranef_chronotype_interval_beta + 2.576*ranef_se_issp_2007_left_right_model2$se_chronotype_interval_beta

View(ranef_se_issp_2007_left_right_model2)

ranef(issp_2007_left_right_model3)
arm::se.ranef(issp_2007_left_right_model3)

ranef(issp_2007_left_right_model3)$country
ranef_issp_2007_left_right_model3 <- data.frame(ranef(issp_2007_left_right_model3)$country)
ranef_issp_2007_left_right_model3 <- subset(ranef_issp_2007_left_right_model3, select = c("chronotype_interval_beta"))
ranef_issp_2007_left_right_model3$country <- row.names(ranef_issp_2007_left_right_model3)
names(ranef_issp_2007_left_right_model3)[names(ranef_issp_2007_left_right_model3)=="chronotype_interval_beta"] <- "ranef_chronotype_interval_beta"


se_ranef_issp_2007_left_right_model3 <- data.frame(arm::se.ranef(issp_2007_left_right_model3)$country)
se_ranef_issp_2007_left_right_model3 <- subset(se_ranef_issp_2007_left_right_model3, select = c("chronotype_interval_beta"))
se_ranef_issp_2007_left_right_model3$country <- row.names(se_ranef_issp_2007_left_right_model3)
names(se_ranef_issp_2007_left_right_model3)[names(se_ranef_issp_2007_left_right_model3)=="chronotype_interval_beta"] <- "se_chronotype_interval_beta"

ranef_se_issp_2007_left_right_model3 <- merge(ranef_issp_2007_left_right_model3, se_ranef_issp_2007_left_right_model3, by="country")

ranef_se_issp_2007_left_right_model3$chrono.cl.90 <- ranef_se_issp_2007_left_right_model3$ranef_chronotype_interval_beta - 1.645*ranef_se_issp_2007_left_right_model3$se_chronotype_interval_beta
ranef_se_issp_2007_left_right_model3$chrono.cu.90 <- ranef_se_issp_2007_left_right_model3$ranef_chronotype_interval_beta + 1.645*ranef_se_issp_2007_left_right_model3$se_chronotype_interval_beta
ranef_se_issp_2007_left_right_model3$chrono.cl.95 <- ranef_se_issp_2007_left_right_model3$ranef_chronotype_interval_beta - 1.96*ranef_se_issp_2007_left_right_model3$se_chronotype_interval_beta
ranef_se_issp_2007_left_right_model3$chrono.cu.95 <- ranef_se_issp_2007_left_right_model3$ranef_chronotype_interval_beta + 1.96*ranef_se_issp_2007_left_right_model3$se_chronotype_interval_beta
ranef_se_issp_2007_left_right_model3$chrono.cl.99 <- ranef_se_issp_2007_left_right_model3$ranef_chronotype_interval_beta - 2.576*ranef_se_issp_2007_left_right_model3$se_chronotype_interval_beta
ranef_se_issp_2007_left_right_model3$chrono.cu.99 <- ranef_se_issp_2007_left_right_model3$ranef_chronotype_interval_beta + 2.576*ranef_se_issp_2007_left_right_model3$se_chronotype_interval_beta

View(ranef_se_issp_2007_left_right_model3)

#### Online Appendices, Table 2A: Multilevel linear modeling of left-right ideological placement without controlling income ####

issp_2007_left_right_model4 <- lmer(PARTY_LR_beta ~ chronotype_interval_beta + urban_rural_area_beta +
                                      sex_beta + age_beta + degree_beta + religiosity_beta + political_interest_beta + (1 + chronotype_interval_beta | country), 
                                    data =  issp_2007)
summary(issp_2007_left_right_model4)
tab_model(issp_2007_left_right_model4, show.ci = 0.95)
round(variancePartition::calcVarPart(issp_2007_left_right_model4)*100,2)
lmerTest::ranova(issp_2007_left_right_model4)

stargazer(issp_2007_left_right_model4,
          type = "html", title=" ", digits=2, out="Tables/no_income_models_issp_2007_left_right.htm",
          model.numbers = F,
          column.labels = c("Model 4"),
          covariate.labels = c("Chronotype", 
                               "Urban-Rural Area of Residence", "Sex: Male (Base: Female)",
                               "Age", "Education",
                               "Religious Attendance", "Level on Interest in Politics",
                               "Intercept"))

tab_model(issp_2007_left_right_model4, show.ci = 0.95)

#### Preparation Online Appendices, Table 2B: Predicted and random effects of chronotype on left-right ideological placement for each country without controlling income ####

coef(issp_2007_left_right_model4)
arm::se.coef(issp_2007_left_right_model4)

coef(issp_2007_left_right_model4)$country
coef_issp_2007_left_right_model4 <- data.frame(coef(issp_2007_left_right_model4)$country)
coef_issp_2007_left_right_model4 <- subset(coef_issp_2007_left_right_model4, select = c("chronotype_interval_beta"))
coef_issp_2007_left_right_model4$country <- row.names(coef_issp_2007_left_right_model4)
names(coef_issp_2007_left_right_model4)[names(coef_issp_2007_left_right_model4)=="chronotype_interval_beta"] <- "coef_chronotype_interval_beta"


se_coef_issp_2007_left_right_model4 <- data.frame(arm::se.coef(issp_2007_left_right_model4)$country)
se_coef_issp_2007_left_right_model4 <- subset(se_coef_issp_2007_left_right_model4, select = c("chronotype_interval_beta"))
se_coef_issp_2007_left_right_model4$country <- row.names(se_coef_issp_2007_left_right_model4)
names(se_coef_issp_2007_left_right_model4)[names(se_coef_issp_2007_left_right_model4)=="chronotype_interval_beta"] <- "se_chronotype_interval_beta"

coef_se_issp_2007_left_right_model4 <- merge(coef_issp_2007_left_right_model4, se_coef_issp_2007_left_right_model4, by="country")

coef_se_issp_2007_left_right_model4$chrono.cl.90 <- coef_se_issp_2007_left_right_model4$coef_chronotype_interval_beta - 1.645*coef_se_issp_2007_left_right_model4$se_chronotype_interval_beta
coef_se_issp_2007_left_right_model4$chrono.cu.90 <- coef_se_issp_2007_left_right_model4$coef_chronotype_interval_beta + 1.645*coef_se_issp_2007_left_right_model4$se_chronotype_interval_beta
coef_se_issp_2007_left_right_model4$chrono.cl.95 <- coef_se_issp_2007_left_right_model4$coef_chronotype_interval_beta - 1.96*coef_se_issp_2007_left_right_model4$se_chronotype_interval_beta
coef_se_issp_2007_left_right_model4$chrono.cu.95 <- coef_se_issp_2007_left_right_model4$coef_chronotype_interval_beta + 1.96*coef_se_issp_2007_left_right_model4$se_chronotype_interval_beta
coef_se_issp_2007_left_right_model4$chrono.cl.99 <- coef_se_issp_2007_left_right_model4$coef_chronotype_interval_beta - 2.576*coef_se_issp_2007_left_right_model4$se_chronotype_interval_beta
coef_se_issp_2007_left_right_model4$chrono.cu.99 <- coef_se_issp_2007_left_right_model4$coef_chronotype_interval_beta + 2.576*coef_se_issp_2007_left_right_model4$se_chronotype_interval_beta

View(coef_se_issp_2007_left_right_model4)

ranef(issp_2007_left_right_model4)
arm::se.ranef(issp_2007_left_right_model4)

ranef(issp_2007_left_right_model4)$country
ranef_issp_2007_left_right_model4 <- data.frame(ranef(issp_2007_left_right_model4)$country)
ranef_issp_2007_left_right_model4 <- subset(ranef_issp_2007_left_right_model4, select = c("chronotype_interval_beta"))
ranef_issp_2007_left_right_model4$country <- row.names(ranef_issp_2007_left_right_model4)
names(ranef_issp_2007_left_right_model4)[names(ranef_issp_2007_left_right_model4)=="chronotype_interval_beta"] <- "ranef_chronotype_interval_beta"


se_ranef_issp_2007_left_right_model4 <- data.frame(arm::se.ranef(issp_2007_left_right_model4)$country)
se_ranef_issp_2007_left_right_model4 <- subset(se_ranef_issp_2007_left_right_model4, select = c("chronotype_interval_beta"))
se_ranef_issp_2007_left_right_model4$country <- row.names(se_ranef_issp_2007_left_right_model4)
names(se_ranef_issp_2007_left_right_model4)[names(se_ranef_issp_2007_left_right_model4)=="chronotype_interval_beta"] <- "se_chronotype_interval_beta"

ranef_se_issp_2007_left_right_model4 <- merge(ranef_issp_2007_left_right_model4, se_ranef_issp_2007_left_right_model4, by="country")

ranef_se_issp_2007_left_right_model4$chrono.cl.90 <- ranef_se_issp_2007_left_right_model4$ranef_chronotype_interval_beta - 1.645*ranef_se_issp_2007_left_right_model4$se_chronotype_interval_beta
ranef_se_issp_2007_left_right_model4$chrono.cu.90 <- ranef_se_issp_2007_left_right_model4$ranef_chronotype_interval_beta + 1.645*ranef_se_issp_2007_left_right_model4$se_chronotype_interval_beta
ranef_se_issp_2007_left_right_model4$chrono.cl.95 <- ranef_se_issp_2007_left_right_model4$ranef_chronotype_interval_beta - 1.96*ranef_se_issp_2007_left_right_model4$se_chronotype_interval_beta
ranef_se_issp_2007_left_right_model4$chrono.cu.95 <- ranef_se_issp_2007_left_right_model4$ranef_chronotype_interval_beta + 1.96*ranef_se_issp_2007_left_right_model4$se_chronotype_interval_beta
ranef_se_issp_2007_left_right_model4$chrono.cl.99 <- ranef_se_issp_2007_left_right_model4$ranef_chronotype_interval_beta - 2.576*ranef_se_issp_2007_left_right_model4$se_chronotype_interval_beta
ranef_se_issp_2007_left_right_model4$chrono.cu.99 <- ranef_se_issp_2007_left_right_model4$ranef_chronotype_interval_beta + 2.576*ranef_se_issp_2007_left_right_model4$se_chronotype_interval_beta

View(ranef_se_issp_2007_left_right_model4)

#### Religious Attendance ####

issp_2007_religiosity_model1 <- lmer(religiosity_beta ~ chronotype_interval_beta + (1 + chronotype_interval_beta | country), 
                                     data =  issp_2007)
summary(issp_2007_religiosity_model1)
tab_model(issp_2007_religiosity_model1, show.ci = 0.95)
round(variancePartition::calcVarPart(issp_2007_religiosity_model1)*100,2)
lmerTest::ranova(issp_2007_religiosity_model1)

issp_2007_religiosity_model2 <- lmer(religiosity_beta ~ chronotype_interval_beta + urban_rural_area_beta + (1 + chronotype_interval_beta  | country), 
                                     data =  issp_2007)
summary(issp_2007_religiosity_model2)
tab_model(issp_2007_religiosity_model2, show.ci = 0.95)
round(variancePartition::calcVarPart(issp_2007_religiosity_model2)*100,2)
lmerTest::ranova(issp_2007_religiosity_model2)

issp_2007_religiosity_model3 <- lmer(religiosity_beta ~ chronotype_interval_beta + urban_rural_area_beta +
                                       sex_beta + age_beta + degree_beta + income_beta + political_interest_beta + (1  + chronotype_interval_beta | country), 
                                     data =  issp_2007)
summary(issp_2007_religiosity_model3)
tab_model(issp_2007_religiosity_model3, show.ci = 0.95)
round(variancePartition::calcVarPart(issp_2007_religiosity_model3)*100,2)
lmerTest::ranova(issp_2007_religiosity_model3)

issp_2007_religiosity_model4 <- lmer(religiosity_beta ~ chronotype_interval_beta + urban_rural_area_beta +
                                       sex_beta + age_beta + degree_beta + income_beta + political_interest_beta + PARTY_LR_beta + (1  + chronotype_interval_beta | country), 
                                     data =  issp_2007)
summary(issp_2007_religiosity_model4)
tab_model(issp_2007_religiosity_model4, show.ci = 0.95)
round(variancePartition::calcVarPart(issp_2007_religiosity_model4)*100,2)
lmerTest::ranova(issp_2007_religiosity_model4)

#### Manuscript, Table 3A: Multilevel linear modeling of religious attendance ####

stargazer(issp_2007_religiosity_model1, issp_2007_religiosity_model2, issp_2007_religiosity_model3, issp_2007_religiosity_model4,
          type = "html", title=" ", digits=2, out="Tables/models_issp_2007_religiosity.htm",
          model.numbers = F,
          column.labels = c("Model 1", "Model 2", "Model 3", "Model 4"),
          covariate.labels = c("Chronotype", 
                               "Urban-Rural Area of Residence", "Sex: Male (Base: Female)",
                               "Age", "Education", "Income",
                               "Level on Interest in Politics", "Left-Right Ideological Placement", 
                               "Intercept"))

tab_model(issp_2007_religiosity_model1, show.ci = 0.95)
tab_model(issp_2007_religiosity_model2, show.ci = 0.95)
tab_model(issp_2007_religiosity_model3, show.ci = 0.95)
tab_model(issp_2007_religiosity_model4, show.ci = 0.95)

#### Preparation for Manuscript, Table 3B: Predicted effects of chronotype on religious attendance for each country ####

coef(issp_2007_religiosity_model1)
arm::se.coef(issp_2007_religiosity_model1)

coef(issp_2007_religiosity_model1)$country
coef_issp_2007_religiosity_model1 <- data.frame(coef(issp_2007_religiosity_model1)$country)
coef_issp_2007_religiosity_model1 <- subset(coef_issp_2007_religiosity_model1, select = c("chronotype_interval_beta"))
coef_issp_2007_religiosity_model1$country <- row.names(coef_issp_2007_religiosity_model1)
names(coef_issp_2007_religiosity_model1)[names(coef_issp_2007_religiosity_model1)=="chronotype_interval_beta"] <- "coef_chronotype_interval_beta"


se_coef_issp_2007_religiosity_model1 <- data.frame(arm::se.coef(issp_2007_religiosity_model1)$country)
se_coef_issp_2007_religiosity_model1 <- subset(se_coef_issp_2007_religiosity_model1, select = c("chronotype_interval_beta"))
se_coef_issp_2007_religiosity_model1$country <- row.names(se_coef_issp_2007_religiosity_model1)
names(se_coef_issp_2007_religiosity_model1)[names(se_coef_issp_2007_religiosity_model1)=="chronotype_interval_beta"] <- "se_chronotype_interval_beta"

coef_se_issp_2007_religiosity_model1 <- merge(coef_issp_2007_religiosity_model1, se_coef_issp_2007_religiosity_model1, by="country")

coef_se_issp_2007_religiosity_model1$chrono.cl.90 <- coef_se_issp_2007_religiosity_model1$coef_chronotype_interval_beta - 1.645*coef_se_issp_2007_religiosity_model1$se_chronotype_interval_beta
coef_se_issp_2007_religiosity_model1$chrono.cu.90 <- coef_se_issp_2007_religiosity_model1$coef_chronotype_interval_beta + 1.645*coef_se_issp_2007_religiosity_model1$se_chronotype_interval_beta
coef_se_issp_2007_religiosity_model1$chrono.cl.95 <- coef_se_issp_2007_religiosity_model1$coef_chronotype_interval_beta - 1.96*coef_se_issp_2007_religiosity_model1$se_chronotype_interval_beta
coef_se_issp_2007_religiosity_model1$chrono.cu.95 <- coef_se_issp_2007_religiosity_model1$coef_chronotype_interval_beta + 1.96*coef_se_issp_2007_religiosity_model1$se_chronotype_interval_beta
coef_se_issp_2007_religiosity_model1$chrono.cl.99 <- coef_se_issp_2007_religiosity_model1$coef_chronotype_interval_beta - 2.576*coef_se_issp_2007_religiosity_model1$se_chronotype_interval_beta
coef_se_issp_2007_religiosity_model1$chrono.cu.99 <- coef_se_issp_2007_religiosity_model1$coef_chronotype_interval_beta + 2.576*coef_se_issp_2007_religiosity_model1$se_chronotype_interval_beta

View(coef_se_issp_2007_religiosity_model1)

coef(issp_2007_religiosity_model2)
arm::se.coef(issp_2007_religiosity_model2)

coef(issp_2007_religiosity_model2)$country
coef_issp_2007_religiosity_model2 <- data.frame(coef(issp_2007_religiosity_model2)$country)
coef_issp_2007_religiosity_model2 <- subset(coef_issp_2007_religiosity_model2, select = c("chronotype_interval_beta"))
coef_issp_2007_religiosity_model2$country <- row.names(coef_issp_2007_religiosity_model2)
names(coef_issp_2007_religiosity_model2)[names(coef_issp_2007_religiosity_model2)=="chronotype_interval_beta"] <- "coef_chronotype_interval_beta"


se_coef_issp_2007_religiosity_model2 <- data.frame(arm::se.coef(issp_2007_religiosity_model2)$country)
se_coef_issp_2007_religiosity_model2 <- subset(se_coef_issp_2007_religiosity_model2, select = c("chronotype_interval_beta"))
se_coef_issp_2007_religiosity_model2$country <- row.names(se_coef_issp_2007_religiosity_model2)
names(se_coef_issp_2007_religiosity_model2)[names(se_coef_issp_2007_religiosity_model2)=="chronotype_interval_beta"] <- "se_chronotype_interval_beta"

coef_se_issp_2007_religiosity_model2 <- merge(coef_issp_2007_religiosity_model2, se_coef_issp_2007_religiosity_model2, by="country")

coef_se_issp_2007_religiosity_model2$chrono.cl.90 <- coef_se_issp_2007_religiosity_model2$coef_chronotype_interval_beta - 1.645*coef_se_issp_2007_religiosity_model2$se_chronotype_interval_beta
coef_se_issp_2007_religiosity_model2$chrono.cu.90 <- coef_se_issp_2007_religiosity_model2$coef_chronotype_interval_beta + 1.645*coef_se_issp_2007_religiosity_model2$se_chronotype_interval_beta
coef_se_issp_2007_religiosity_model2$chrono.cl.95 <- coef_se_issp_2007_religiosity_model2$coef_chronotype_interval_beta - 1.96*coef_se_issp_2007_religiosity_model2$se_chronotype_interval_beta
coef_se_issp_2007_religiosity_model2$chrono.cu.95 <- coef_se_issp_2007_religiosity_model2$coef_chronotype_interval_beta + 1.96*coef_se_issp_2007_religiosity_model2$se_chronotype_interval_beta
coef_se_issp_2007_religiosity_model2$chrono.cl.99 <- coef_se_issp_2007_religiosity_model2$coef_chronotype_interval_beta - 2.576*coef_se_issp_2007_religiosity_model2$se_chronotype_interval_beta
coef_se_issp_2007_religiosity_model2$chrono.cu.99 <- coef_se_issp_2007_religiosity_model2$coef_chronotype_interval_beta + 2.576*coef_se_issp_2007_religiosity_model2$se_chronotype_interval_beta

View(coef_se_issp_2007_religiosity_model2)

coef(issp_2007_religiosity_model3)
arm::se.coef(issp_2007_religiosity_model3)

coef(issp_2007_religiosity_model3)$country
coef_issp_2007_religiosity_model3 <- data.frame(coef(issp_2007_religiosity_model3)$country)
coef_issp_2007_religiosity_model3 <- subset(coef_issp_2007_religiosity_model3, select = c("chronotype_interval_beta"))
coef_issp_2007_religiosity_model3$country <- row.names(coef_issp_2007_religiosity_model3)
names(coef_issp_2007_religiosity_model3)[names(coef_issp_2007_religiosity_model3)=="chronotype_interval_beta"] <- "coef_chronotype_interval_beta"


se_coef_issp_2007_religiosity_model3 <- data.frame(arm::se.coef(issp_2007_religiosity_model3)$country)
se_coef_issp_2007_religiosity_model3 <- subset(se_coef_issp_2007_religiosity_model3, select = c("chronotype_interval_beta"))
se_coef_issp_2007_religiosity_model3$country <- row.names(se_coef_issp_2007_religiosity_model3)
names(se_coef_issp_2007_religiosity_model3)[names(se_coef_issp_2007_religiosity_model3)=="chronotype_interval_beta"] <- "se_chronotype_interval_beta"

coef_se_issp_2007_religiosity_model3 <- merge(coef_issp_2007_religiosity_model3, se_coef_issp_2007_religiosity_model3, by="country")

coef_se_issp_2007_religiosity_model3$chrono.cl.90 <- coef_se_issp_2007_religiosity_model3$coef_chronotype_interval_beta - 1.645*coef_se_issp_2007_religiosity_model3$se_chronotype_interval_beta
coef_se_issp_2007_religiosity_model3$chrono.cu.90 <- coef_se_issp_2007_religiosity_model3$coef_chronotype_interval_beta + 1.645*coef_se_issp_2007_religiosity_model3$se_chronotype_interval_beta
coef_se_issp_2007_religiosity_model3$chrono.cl.95 <- coef_se_issp_2007_religiosity_model3$coef_chronotype_interval_beta - 1.96*coef_se_issp_2007_religiosity_model3$se_chronotype_interval_beta
coef_se_issp_2007_religiosity_model3$chrono.cu.95 <- coef_se_issp_2007_religiosity_model3$coef_chronotype_interval_beta + 1.96*coef_se_issp_2007_religiosity_model3$se_chronotype_interval_beta
coef_se_issp_2007_religiosity_model3$chrono.cl.99 <- coef_se_issp_2007_religiosity_model3$coef_chronotype_interval_beta - 2.576*coef_se_issp_2007_religiosity_model3$se_chronotype_interval_beta
coef_se_issp_2007_religiosity_model3$chrono.cu.99 <- coef_se_issp_2007_religiosity_model3$coef_chronotype_interval_beta + 2.576*coef_se_issp_2007_religiosity_model3$se_chronotype_interval_beta

View(coef_se_issp_2007_religiosity_model3)

coef(issp_2007_religiosity_model4)
arm::se.coef(issp_2007_religiosity_model4)

coef(issp_2007_religiosity_model4)$country
coef_issp_2007_religiosity_model4 <- data.frame(coef(issp_2007_religiosity_model4)$country)
coef_issp_2007_religiosity_model4 <- subset(coef_issp_2007_religiosity_model4, select = c("chronotype_interval_beta"))
coef_issp_2007_religiosity_model4$country <- row.names(coef_issp_2007_religiosity_model4)
names(coef_issp_2007_religiosity_model4)[names(coef_issp_2007_religiosity_model4)=="chronotype_interval_beta"] <- "coef_chronotype_interval_beta"


se_coef_issp_2007_religiosity_model4 <- data.frame(arm::se.coef(issp_2007_religiosity_model4)$country)
se_coef_issp_2007_religiosity_model4 <- subset(se_coef_issp_2007_religiosity_model4, select = c("chronotype_interval_beta"))
se_coef_issp_2007_religiosity_model4$country <- row.names(se_coef_issp_2007_religiosity_model4)
names(se_coef_issp_2007_religiosity_model4)[names(se_coef_issp_2007_religiosity_model4)=="chronotype_interval_beta"] <- "se_chronotype_interval_beta"

coef_se_issp_2007_religiosity_model4 <- merge(coef_issp_2007_religiosity_model4, se_coef_issp_2007_religiosity_model4, by="country")

coef_se_issp_2007_religiosity_model4$chrono.cl.90 <- coef_se_issp_2007_religiosity_model4$coef_chronotype_interval_beta - 1.645*coef_se_issp_2007_religiosity_model4$se_chronotype_interval_beta
coef_se_issp_2007_religiosity_model4$chrono.cu.90 <- coef_se_issp_2007_religiosity_model4$coef_chronotype_interval_beta + 1.645*coef_se_issp_2007_religiosity_model4$se_chronotype_interval_beta
coef_se_issp_2007_religiosity_model4$chrono.cl.95 <- coef_se_issp_2007_religiosity_model4$coef_chronotype_interval_beta - 1.96*coef_se_issp_2007_religiosity_model4$se_chronotype_interval_beta
coef_se_issp_2007_religiosity_model4$chrono.cu.95 <- coef_se_issp_2007_religiosity_model4$coef_chronotype_interval_beta + 1.96*coef_se_issp_2007_religiosity_model4$se_chronotype_interval_beta
coef_se_issp_2007_religiosity_model4$chrono.cl.99 <- coef_se_issp_2007_religiosity_model4$coef_chronotype_interval_beta - 2.576*coef_se_issp_2007_religiosity_model4$se_chronotype_interval_beta
coef_se_issp_2007_religiosity_model4$chrono.cu.99 <- coef_se_issp_2007_religiosity_model4$coef_chronotype_interval_beta + 2.576*coef_se_issp_2007_religiosity_model4$se_chronotype_interval_beta

View(coef_se_issp_2007_religiosity_model4)

#### Preparation Online Appendices, Table 3: Random effects of chronotype on religious attendance for each country ####

ranef(issp_2007_religiosity_model1)
arm::se.ranef(issp_2007_religiosity_model1)

ranef(issp_2007_religiosity_model1)$country
ranef_issp_2007_religiosity_model1 <- data.frame(ranef(issp_2007_religiosity_model1)$country)
ranef_issp_2007_religiosity_model1 <- subset(ranef_issp_2007_religiosity_model1, select = c("chronotype_interval_beta"))
ranef_issp_2007_religiosity_model1$country <- row.names(ranef_issp_2007_religiosity_model1)
names(ranef_issp_2007_religiosity_model1)[names(ranef_issp_2007_religiosity_model1)=="chronotype_interval_beta"] <- "ranef_chronotype_interval_beta"


se_ranef_issp_2007_religiosity_model1 <- data.frame(arm::se.ranef(issp_2007_religiosity_model1)$country)
se_ranef_issp_2007_religiosity_model1 <- subset(se_ranef_issp_2007_religiosity_model1, select = c("chronotype_interval_beta"))
se_ranef_issp_2007_religiosity_model1$country <- row.names(se_ranef_issp_2007_religiosity_model1)
names(se_ranef_issp_2007_religiosity_model1)[names(se_ranef_issp_2007_religiosity_model1)=="chronotype_interval_beta"] <- "se_chronotype_interval_beta"

ranef_se_issp_2007_religiosity_model1 <- merge(ranef_issp_2007_religiosity_model1, se_ranef_issp_2007_religiosity_model1, by="country")

ranef_se_issp_2007_religiosity_model1$chrono.cl.90 <- ranef_se_issp_2007_religiosity_model1$ranef_chronotype_interval_beta - 1.645*ranef_se_issp_2007_religiosity_model1$se_chronotype_interval_beta
ranef_se_issp_2007_religiosity_model1$chrono.cu.90 <- ranef_se_issp_2007_religiosity_model1$ranef_chronotype_interval_beta + 1.645*ranef_se_issp_2007_religiosity_model1$se_chronotype_interval_beta
ranef_se_issp_2007_religiosity_model1$chrono.cl.95 <- ranef_se_issp_2007_religiosity_model1$ranef_chronotype_interval_beta - 1.96*ranef_se_issp_2007_religiosity_model1$se_chronotype_interval_beta
ranef_se_issp_2007_religiosity_model1$chrono.cu.95 <- ranef_se_issp_2007_religiosity_model1$ranef_chronotype_interval_beta + 1.96*ranef_se_issp_2007_religiosity_model1$se_chronotype_interval_beta
ranef_se_issp_2007_religiosity_model1$chrono.cl.99 <- ranef_se_issp_2007_religiosity_model1$ranef_chronotype_interval_beta - 2.576*ranef_se_issp_2007_religiosity_model1$se_chronotype_interval_beta
ranef_se_issp_2007_religiosity_model1$chrono.cu.99 <- ranef_se_issp_2007_religiosity_model1$ranef_chronotype_interval_beta + 2.576*ranef_se_issp_2007_religiosity_model1$se_chronotype_interval_beta

View(ranef_se_issp_2007_religiosity_model1)

ranef(issp_2007_religiosity_model2)
arm::se.ranef(issp_2007_religiosity_model2)

ranef(issp_2007_religiosity_model2)$country
ranef_issp_2007_religiosity_model2 <- data.frame(ranef(issp_2007_religiosity_model2)$country)
ranef_issp_2007_religiosity_model2 <- subset(ranef_issp_2007_religiosity_model2, select = c("chronotype_interval_beta"))
ranef_issp_2007_religiosity_model2$country <- row.names(ranef_issp_2007_religiosity_model2)
names(ranef_issp_2007_religiosity_model2)[names(ranef_issp_2007_religiosity_model2)=="chronotype_interval_beta"] <- "ranef_chronotype_interval_beta"


se_ranef_issp_2007_religiosity_model2 <- data.frame(arm::se.ranef(issp_2007_religiosity_model2)$country)
se_ranef_issp_2007_religiosity_model2 <- subset(se_ranef_issp_2007_religiosity_model2, select = c("chronotype_interval_beta"))
se_ranef_issp_2007_religiosity_model2$country <- row.names(se_ranef_issp_2007_religiosity_model2)
names(se_ranef_issp_2007_religiosity_model2)[names(se_ranef_issp_2007_religiosity_model2)=="chronotype_interval_beta"] <- "se_chronotype_interval_beta"

ranef_se_issp_2007_religiosity_model2 <- merge(ranef_issp_2007_religiosity_model2, se_ranef_issp_2007_religiosity_model2, by="country")

ranef_se_issp_2007_religiosity_model2$chrono.cl.90 <- ranef_se_issp_2007_religiosity_model2$ranef_chronotype_interval_beta - 1.645*ranef_se_issp_2007_religiosity_model2$se_chronotype_interval_beta
ranef_se_issp_2007_religiosity_model2$chrono.cu.90 <- ranef_se_issp_2007_religiosity_model2$ranef_chronotype_interval_beta + 1.645*ranef_se_issp_2007_religiosity_model2$se_chronotype_interval_beta
ranef_se_issp_2007_religiosity_model2$chrono.cl.95 <- ranef_se_issp_2007_religiosity_model2$ranef_chronotype_interval_beta - 1.96*ranef_se_issp_2007_religiosity_model2$se_chronotype_interval_beta
ranef_se_issp_2007_religiosity_model2$chrono.cu.95 <- ranef_se_issp_2007_religiosity_model2$ranef_chronotype_interval_beta + 1.96*ranef_se_issp_2007_religiosity_model2$se_chronotype_interval_beta
ranef_se_issp_2007_religiosity_model2$chrono.cl.99 <- ranef_se_issp_2007_religiosity_model2$ranef_chronotype_interval_beta - 2.576*ranef_se_issp_2007_religiosity_model2$se_chronotype_interval_beta
ranef_se_issp_2007_religiosity_model2$chrono.cu.99 <- ranef_se_issp_2007_religiosity_model2$ranef_chronotype_interval_beta + 2.576*ranef_se_issp_2007_religiosity_model2$se_chronotype_interval_beta

View(ranef_se_issp_2007_religiosity_model2)

ranef(issp_2007_religiosity_model3)
arm::se.ranef(issp_2007_religiosity_model3)

ranef(issp_2007_religiosity_model3)$country
ranef_issp_2007_religiosity_model3 <- data.frame(ranef(issp_2007_religiosity_model3)$country)
ranef_issp_2007_religiosity_model3 <- subset(ranef_issp_2007_religiosity_model3, select = c("chronotype_interval_beta"))
ranef_issp_2007_religiosity_model3$country <- row.names(ranef_issp_2007_religiosity_model3)
names(ranef_issp_2007_religiosity_model3)[names(ranef_issp_2007_religiosity_model3)=="chronotype_interval_beta"] <- "ranef_chronotype_interval_beta"


se_ranef_issp_2007_religiosity_model3 <- data.frame(arm::se.ranef(issp_2007_religiosity_model3)$country)
se_ranef_issp_2007_religiosity_model3 <- subset(se_ranef_issp_2007_religiosity_model3, select = c("chronotype_interval_beta"))
se_ranef_issp_2007_religiosity_model3$country <- row.names(se_ranef_issp_2007_religiosity_model3)
names(se_ranef_issp_2007_religiosity_model3)[names(se_ranef_issp_2007_religiosity_model3)=="chronotype_interval_beta"] <- "se_chronotype_interval_beta"

ranef_se_issp_2007_religiosity_model3 <- merge(ranef_issp_2007_religiosity_model3, se_ranef_issp_2007_religiosity_model3, by="country")

ranef_se_issp_2007_religiosity_model3$chrono.cl.90 <- ranef_se_issp_2007_religiosity_model3$ranef_chronotype_interval_beta - 1.645*ranef_se_issp_2007_religiosity_model3$se_chronotype_interval_beta
ranef_se_issp_2007_religiosity_model3$chrono.cu.90 <- ranef_se_issp_2007_religiosity_model3$ranef_chronotype_interval_beta + 1.645*ranef_se_issp_2007_religiosity_model3$se_chronotype_interval_beta
ranef_se_issp_2007_religiosity_model3$chrono.cl.95 <- ranef_se_issp_2007_religiosity_model3$ranef_chronotype_interval_beta - 1.96*ranef_se_issp_2007_religiosity_model3$se_chronotype_interval_beta
ranef_se_issp_2007_religiosity_model3$chrono.cu.95 <- ranef_se_issp_2007_religiosity_model3$ranef_chronotype_interval_beta + 1.96*ranef_se_issp_2007_religiosity_model3$se_chronotype_interval_beta
ranef_se_issp_2007_religiosity_model3$chrono.cl.99 <- ranef_se_issp_2007_religiosity_model3$ranef_chronotype_interval_beta - 2.576*ranef_se_issp_2007_religiosity_model3$se_chronotype_interval_beta
ranef_se_issp_2007_religiosity_model3$chrono.cu.99 <- ranef_se_issp_2007_religiosity_model3$ranef_chronotype_interval_beta + 2.576*ranef_se_issp_2007_religiosity_model3$se_chronotype_interval_beta

View(ranef_se_issp_2007_religiosity_model3)

ranef(issp_2007_religiosity_model4)
arm::se.ranef(issp_2007_religiosity_model4)

ranef(issp_2007_religiosity_model4)$country
ranef_issp_2007_religiosity_model4 <- data.frame(ranef(issp_2007_religiosity_model4)$country)
ranef_issp_2007_religiosity_model4 <- subset(ranef_issp_2007_religiosity_model4, select = c("chronotype_interval_beta"))
ranef_issp_2007_religiosity_model4$country <- row.names(ranef_issp_2007_religiosity_model4)
names(ranef_issp_2007_religiosity_model4)[names(ranef_issp_2007_religiosity_model4)=="chronotype_interval_beta"] <- "ranef_chronotype_interval_beta"


se_ranef_issp_2007_religiosity_model4 <- data.frame(arm::se.ranef(issp_2007_religiosity_model4)$country)
se_ranef_issp_2007_religiosity_model4 <- subset(se_ranef_issp_2007_religiosity_model4, select = c("chronotype_interval_beta"))
se_ranef_issp_2007_religiosity_model4$country <- row.names(se_ranef_issp_2007_religiosity_model4)
names(se_ranef_issp_2007_religiosity_model4)[names(se_ranef_issp_2007_religiosity_model4)=="chronotype_interval_beta"] <- "se_chronotype_interval_beta"

ranef_se_issp_2007_religiosity_model4 <- merge(ranef_issp_2007_religiosity_model4, se_ranef_issp_2007_religiosity_model4, by="country")

ranef_se_issp_2007_religiosity_model4$chrono.cl.90 <- ranef_se_issp_2007_religiosity_model4$ranef_chronotype_interval_beta - 1.645*ranef_se_issp_2007_religiosity_model4$se_chronotype_interval_beta
ranef_se_issp_2007_religiosity_model4$chrono.cu.90 <- ranef_se_issp_2007_religiosity_model4$ranef_chronotype_interval_beta + 1.645*ranef_se_issp_2007_religiosity_model4$se_chronotype_interval_beta
ranef_se_issp_2007_religiosity_model4$chrono.cl.95 <- ranef_se_issp_2007_religiosity_model4$ranef_chronotype_interval_beta - 1.96*ranef_se_issp_2007_religiosity_model4$se_chronotype_interval_beta
ranef_se_issp_2007_religiosity_model4$chrono.cu.95 <- ranef_se_issp_2007_religiosity_model4$ranef_chronotype_interval_beta + 1.96*ranef_se_issp_2007_religiosity_model4$se_chronotype_interval_beta
ranef_se_issp_2007_religiosity_model4$chrono.cl.99 <- ranef_se_issp_2007_religiosity_model4$ranef_chronotype_interval_beta - 2.576*ranef_se_issp_2007_religiosity_model4$se_chronotype_interval_beta
ranef_se_issp_2007_religiosity_model4$chrono.cu.99 <- ranef_se_issp_2007_religiosity_model4$ranef_chronotype_interval_beta + 2.576*ranef_se_issp_2007_religiosity_model4$se_chronotype_interval_beta

View(ranef_se_issp_2007_religiosity_model4)

#### Online Appendices, Table 4A: Multilevel linear modeling of religious attendance without controlling age ####

issp_2007_religiosity_model5 <- lmer(religiosity_beta ~ chronotype_interval_beta + urban_rural_area_beta +
                                       sex_beta + degree_beta + income_beta + political_interest_beta + (1  + chronotype_interval_beta | country), 
                                     data =  issp_2007)
summary(issp_2007_religiosity_model5)
tab_model(issp_2007_religiosity_model5, show.ci = 0.95)
round(variancePartition::calcVarPart(issp_2007_religiosity_model5)*100,2)
lmerTest::ranova(issp_2007_religiosity_model5)

issp_2007_religiosity_model6 <- lmer(religiosity_beta ~ chronotype_interval_beta + urban_rural_area_beta +
                                       sex_beta + degree_beta + income_beta + political_interest_beta + PARTY_LR_beta + (1  + chronotype_interval_beta | country), 
                                     data =  issp_2007)
summary(issp_2007_religiosity_model6)
tab_model(issp_2007_religiosity_model6, show.ci = 0.95)
round(variancePartition::calcVarPart(issp_2007_religiosity_model6)*100,2)
lmerTest::ranova(issp_2007_religiosity_model6)

stargazer(issp_2007_religiosity_model5, issp_2007_religiosity_model6, 
          type = "html", title=" ", digits=2, out="Tables/no_age_models_issp_2007_religiosity.htm",
          model.numbers = F,
          column.labels = c("Model 5", "Model 6"),
          covariate.labels = c("Chronotype", 
                               "Urban-Rural Area of Residence", "Sex: Male (Base: Female)",
                               "Education", "Income",
                               "Level on Interest in Politics", "Left-Right Ideological Placement", 
                               "Intercept"))

tab_model(issp_2007_religiosity_model5, show.ci = 0.95)
tab_model(issp_2007_religiosity_model6, show.ci = 0.95)

#### Preparation Online Appendices, Table 4B: Predicted and random effects of chronotype on religious attendance for each country without controlling age ####

coef(issp_2007_religiosity_model5)
arm::se.coef(issp_2007_religiosity_model5)

coef(issp_2007_religiosity_model5)$country
coef_issp_2007_religiosity_model5 <- data.frame(coef(issp_2007_religiosity_model5)$country)
coef_issp_2007_religiosity_model5 <- subset(coef_issp_2007_religiosity_model5, select = c("chronotype_interval_beta"))
coef_issp_2007_religiosity_model5$country <- row.names(coef_issp_2007_religiosity_model5)
names(coef_issp_2007_religiosity_model5)[names(coef_issp_2007_religiosity_model5)=="chronotype_interval_beta"] <- "coef_chronotype_interval_beta"


se_coef_issp_2007_religiosity_model5 <- data.frame(arm::se.coef(issp_2007_religiosity_model5)$country)
se_coef_issp_2007_religiosity_model5 <- subset(se_coef_issp_2007_religiosity_model5, select = c("chronotype_interval_beta"))
se_coef_issp_2007_religiosity_model5$country <- row.names(se_coef_issp_2007_religiosity_model5)
names(se_coef_issp_2007_religiosity_model5)[names(se_coef_issp_2007_religiosity_model5)=="chronotype_interval_beta"] <- "se_chronotype_interval_beta"

coef_se_issp_2007_religiosity_model5 <- merge(coef_issp_2007_religiosity_model5, se_coef_issp_2007_religiosity_model5, by="country")

coef_se_issp_2007_religiosity_model5$chrono.cl.90 <- coef_se_issp_2007_religiosity_model5$coef_chronotype_interval_beta - 1.645*coef_se_issp_2007_religiosity_model5$se_chronotype_interval_beta
coef_se_issp_2007_religiosity_model5$chrono.cu.90 <- coef_se_issp_2007_religiosity_model5$coef_chronotype_interval_beta + 1.645*coef_se_issp_2007_religiosity_model5$se_chronotype_interval_beta
coef_se_issp_2007_religiosity_model5$chrono.cl.95 <- coef_se_issp_2007_religiosity_model5$coef_chronotype_interval_beta - 1.96*coef_se_issp_2007_religiosity_model5$se_chronotype_interval_beta
coef_se_issp_2007_religiosity_model5$chrono.cu.95 <- coef_se_issp_2007_religiosity_model5$coef_chronotype_interval_beta + 1.96*coef_se_issp_2007_religiosity_model5$se_chronotype_interval_beta
coef_se_issp_2007_religiosity_model5$chrono.cl.99 <- coef_se_issp_2007_religiosity_model5$coef_chronotype_interval_beta - 2.576*coef_se_issp_2007_religiosity_model5$se_chronotype_interval_beta
coef_se_issp_2007_religiosity_model5$chrono.cu.99 <- coef_se_issp_2007_religiosity_model5$coef_chronotype_interval_beta + 2.576*coef_se_issp_2007_religiosity_model5$se_chronotype_interval_beta

View(coef_se_issp_2007_religiosity_model5)

ranef(issp_2007_religiosity_model5)
arm::se.ranef(issp_2007_religiosity_model5)

ranef(issp_2007_religiosity_model5)$country
ranef_issp_2007_religiosity_model5 <- data.frame(ranef(issp_2007_religiosity_model5)$country)
ranef_issp_2007_religiosity_model5 <- subset(ranef_issp_2007_religiosity_model5, select = c("chronotype_interval_beta"))
ranef_issp_2007_religiosity_model5$country <- row.names(ranef_issp_2007_religiosity_model5)
names(ranef_issp_2007_religiosity_model5)[names(ranef_issp_2007_religiosity_model5)=="chronotype_interval_beta"] <- "ranef_chronotype_interval_beta"


se_ranef_issp_2007_religiosity_model5 <- data.frame(arm::se.ranef(issp_2007_religiosity_model5)$country)
se_ranef_issp_2007_religiosity_model5 <- subset(se_ranef_issp_2007_religiosity_model5, select = c("chronotype_interval_beta"))
se_ranef_issp_2007_religiosity_model5$country <- row.names(se_ranef_issp_2007_religiosity_model5)
names(se_ranef_issp_2007_religiosity_model5)[names(se_ranef_issp_2007_religiosity_model5)=="chronotype_interval_beta"] <- "se_chronotype_interval_beta"

ranef_se_issp_2007_religiosity_model5 <- merge(ranef_issp_2007_religiosity_model5, se_ranef_issp_2007_religiosity_model5, by="country")

ranef_se_issp_2007_religiosity_model5$chrono.cl.90 <- ranef_se_issp_2007_religiosity_model5$ranef_chronotype_interval_beta - 1.645*ranef_se_issp_2007_religiosity_model5$se_chronotype_interval_beta
ranef_se_issp_2007_religiosity_model5$chrono.cu.90 <- ranef_se_issp_2007_religiosity_model5$ranef_chronotype_interval_beta + 1.645*ranef_se_issp_2007_religiosity_model5$se_chronotype_interval_beta
ranef_se_issp_2007_religiosity_model5$chrono.cl.95 <- ranef_se_issp_2007_religiosity_model5$ranef_chronotype_interval_beta - 1.96*ranef_se_issp_2007_religiosity_model5$se_chronotype_interval_beta
ranef_se_issp_2007_religiosity_model5$chrono.cu.95 <- ranef_se_issp_2007_religiosity_model5$ranef_chronotype_interval_beta + 1.96*ranef_se_issp_2007_religiosity_model5$se_chronotype_interval_beta
ranef_se_issp_2007_religiosity_model5$chrono.cl.99 <- ranef_se_issp_2007_religiosity_model5$ranef_chronotype_interval_beta - 2.576*ranef_se_issp_2007_religiosity_model5$se_chronotype_interval_beta
ranef_se_issp_2007_religiosity_model5$chrono.cu.99 <- ranef_se_issp_2007_religiosity_model5$ranef_chronotype_interval_beta + 2.576*ranef_se_issp_2007_religiosity_model5$se_chronotype_interval_beta

View(ranef_se_issp_2007_religiosity_model5)

coef(issp_2007_religiosity_model6)
arm::se.coef(issp_2007_religiosity_model6)

coef(issp_2007_religiosity_model6)$country
coef_issp_2007_religiosity_model6 <- data.frame(coef(issp_2007_religiosity_model6)$country)
coef_issp_2007_religiosity_model6 <- subset(coef_issp_2007_religiosity_model6, select = c("chronotype_interval_beta"))
coef_issp_2007_religiosity_model6$country <- row.names(coef_issp_2007_religiosity_model6)
names(coef_issp_2007_religiosity_model6)[names(coef_issp_2007_religiosity_model6)=="chronotype_interval_beta"] <- "coef_chronotype_interval_beta"


se_coef_issp_2007_religiosity_model6 <- data.frame(arm::se.coef(issp_2007_religiosity_model6)$country)
se_coef_issp_2007_religiosity_model6 <- subset(se_coef_issp_2007_religiosity_model6, select = c("chronotype_interval_beta"))
se_coef_issp_2007_religiosity_model6$country <- row.names(se_coef_issp_2007_religiosity_model6)
names(se_coef_issp_2007_religiosity_model6)[names(se_coef_issp_2007_religiosity_model6)=="chronotype_interval_beta"] <- "se_chronotype_interval_beta"

coef_se_issp_2007_religiosity_model6 <- merge(coef_issp_2007_religiosity_model6, se_coef_issp_2007_religiosity_model6, by="country")

coef_se_issp_2007_religiosity_model6$chrono.cl.90 <- coef_se_issp_2007_religiosity_model6$coef_chronotype_interval_beta - 1.645*coef_se_issp_2007_religiosity_model6$se_chronotype_interval_beta
coef_se_issp_2007_religiosity_model6$chrono.cu.90 <- coef_se_issp_2007_religiosity_model6$coef_chronotype_interval_beta + 1.645*coef_se_issp_2007_religiosity_model6$se_chronotype_interval_beta
coef_se_issp_2007_religiosity_model6$chrono.cl.95 <- coef_se_issp_2007_religiosity_model6$coef_chronotype_interval_beta - 1.96*coef_se_issp_2007_religiosity_model6$se_chronotype_interval_beta
coef_se_issp_2007_religiosity_model6$chrono.cu.95 <- coef_se_issp_2007_religiosity_model6$coef_chronotype_interval_beta + 1.96*coef_se_issp_2007_religiosity_model6$se_chronotype_interval_beta
coef_se_issp_2007_religiosity_model6$chrono.cl.99 <- coef_se_issp_2007_religiosity_model6$coef_chronotype_interval_beta - 2.576*coef_se_issp_2007_religiosity_model6$se_chronotype_interval_beta
coef_se_issp_2007_religiosity_model6$chrono.cu.99 <- coef_se_issp_2007_religiosity_model6$coef_chronotype_interval_beta + 2.576*coef_se_issp_2007_religiosity_model6$se_chronotype_interval_beta

View(coef_se_issp_2007_religiosity_model6)

ranef(issp_2007_religiosity_model6)
arm::se.ranef(issp_2007_religiosity_model6)

ranef(issp_2007_religiosity_model6)$country
ranef_issp_2007_religiosity_model6 <- data.frame(ranef(issp_2007_religiosity_model6)$country)
ranef_issp_2007_religiosity_model6 <- subset(ranef_issp_2007_religiosity_model6, select = c("chronotype_interval_beta"))
ranef_issp_2007_religiosity_model6$country <- row.names(ranef_issp_2007_religiosity_model6)
names(ranef_issp_2007_religiosity_model6)[names(ranef_issp_2007_religiosity_model6)=="chronotype_interval_beta"] <- "ranef_chronotype_interval_beta"


se_ranef_issp_2007_religiosity_model6 <- data.frame(arm::se.ranef(issp_2007_religiosity_model6)$country)
se_ranef_issp_2007_religiosity_model6 <- subset(se_ranef_issp_2007_religiosity_model6, select = c("chronotype_interval_beta"))
se_ranef_issp_2007_religiosity_model6$country <- row.names(se_ranef_issp_2007_religiosity_model6)
names(se_ranef_issp_2007_religiosity_model6)[names(se_ranef_issp_2007_religiosity_model6)=="chronotype_interval_beta"] <- "se_chronotype_interval_beta"

ranef_se_issp_2007_religiosity_model6 <- merge(ranef_issp_2007_religiosity_model6, se_ranef_issp_2007_religiosity_model6, by="country")

ranef_se_issp_2007_religiosity_model6$chrono.cl.90 <- ranef_se_issp_2007_religiosity_model6$ranef_chronotype_interval_beta - 1.645*ranef_se_issp_2007_religiosity_model6$se_chronotype_interval_beta
ranef_se_issp_2007_religiosity_model6$chrono.cu.90 <- ranef_se_issp_2007_religiosity_model6$ranef_chronotype_interval_beta + 1.645*ranef_se_issp_2007_religiosity_model6$se_chronotype_interval_beta
ranef_se_issp_2007_religiosity_model6$chrono.cl.95 <- ranef_se_issp_2007_religiosity_model6$ranef_chronotype_interval_beta - 1.96*ranef_se_issp_2007_religiosity_model6$se_chronotype_interval_beta
ranef_se_issp_2007_religiosity_model6$chrono.cu.95 <- ranef_se_issp_2007_religiosity_model6$ranef_chronotype_interval_beta + 1.96*ranef_se_issp_2007_religiosity_model6$se_chronotype_interval_beta
ranef_se_issp_2007_religiosity_model6$chrono.cl.99 <- ranef_se_issp_2007_religiosity_model6$ranef_chronotype_interval_beta - 2.576*ranef_se_issp_2007_religiosity_model6$se_chronotype_interval_beta
ranef_se_issp_2007_religiosity_model6$chrono.cu.99 <- ranef_se_issp_2007_religiosity_model6$ranef_chronotype_interval_beta + 2.576*ranef_se_issp_2007_religiosity_model6$se_chronotype_interval_beta

View(ranef_se_issp_2007_religiosity_model6)

#### Exploratory Analyses 1 ####

table(issp_2007$mid_field_season)
issp_2007$mid_field_season[issp_2007$country=="Greece"] <- "Winter"

issp_2007_left_right_model_day <- lmer(PARTY_LR_beta ~ chronotype_interval_beta + 
                                         urban_rural_area_beta +
                                         sex_beta + age_beta + degree_beta + income_beta + religiosity_beta + political_interest_beta +
                                         (1 + chronotype_interval_beta | country) + (0 + chronotype_interval_beta | mid_field_season/day_off), 
                                       data =  issp_2007)
summary(issp_2007_left_right_model_day)
tab_model(issp_2007_left_right_model_day, show.ci = 0.95)

issp_2007_religiosity_model_day <- lmer(religiosity_beta ~ chronotype_interval_beta +  
                                          urban_rural_area_beta +
                                          sex_beta + age_beta + degree_beta + income_beta + PARTY_LR_beta + political_interest_beta +
                                          (1 + chronotype_interval_beta | country) + (0 + chronotype_interval_beta | mid_field_season/day_off), 
                                        data =  issp_2007)
summary(issp_2007_religiosity_model_day)
tab_model(issp_2007_religiosity_model_day, show.ci = 0.95)

#library(dfoptim)
#library(optimx)
#library(parallel)
#library(minqa)

#diff_optims <- allFit(issp_2007_religiosity_model_day, maxfun = 1e5, parallel = 'multicore')
#is.OK <- sapply(diff_optims, is, "merMod")
#diff_optims.OK <- diff_optims[is.OK]
#lapply(diff_optims.OK,function(x) x@optinfo$conv$lme4$messages)

#issp_2007_religiosity_model_day <- lmer(religiosity_beta ~ chronotype_interval_beta +  
#                                          urban_rural_area_beta +
#                                          sex_beta + age_beta + degree_beta + income_beta + PARTY_LR_beta + political_interest_beta +
#                                          (1 + chronotype_interval_beta | country) + (0 + chronotype_interval_beta | mid_field_season/day_off), 
#                                        data =  issp_2007,
#                                        control = lmerControl(optimizer = "nlminbwrap"))
#summary(issp_2007_religiosity_model_day)
#tab_model(issp_2007_religiosity_model_day, show.ci = 0.95)

#coef(issp_2007_religiosity_model_day)

#### Online Appendices, Table 5A: Multilevel linear modeling of left-right ideological placement and religious attendance additionally accounting for days of sleep measurement and seasons ####

stargazer(issp_2007_left_right_model_day, issp_2007_religiosity_model_day,
          type = "html", title=" ", digits=2, out="Tables/models_issp_2007_day.htm",
          model.numbers = F,
          column.labels = c("Model 1", "Model 2"),
          covariate.labels = c("Chronotype", 
                               "Urban-Rural Area of Residence",
                               "Sex: Male (Base: Female)",
                               "Age",
                               "Education", 
                               "Income",
                               "Religious Attendance",
                               "Left-Right Ideological Placement",
                               "Level on Interest in Politics",
                               "Intercept"))

tab_model(issp_2007_left_right_model_day, show.ci = .95)
tab_model(issp_2007_religiosity_model_day, show.ci = .95)

#### Preparation Online Appendices, Table 5B: Predicted and random effects of chronotype on left-right ideological placement and religious attendance for each country by additionally accounting for days of sleep measurement and seasons ####

coef(issp_2007_left_right_model_day)
arm::se.coef(issp_2007_left_right_model_day)

ranef(issp_2007_left_right_model_day)
arm::se.ranef(issp_2007_left_right_model_day)

coef(issp_2007_left_right_model_day)$country
coef_issp_2007_left_right_model_day <- data.frame(coef(issp_2007_left_right_model_day)$country)
coef_issp_2007_left_right_model_day <- subset(coef_issp_2007_left_right_model_day, select = c("chronotype_interval_beta"))
coef_issp_2007_left_right_model_day$country <- row.names(coef_issp_2007_left_right_model_day)
names(coef_issp_2007_left_right_model_day)[names(coef_issp_2007_left_right_model_day)=="chronotype_interval_beta"] <- "coef_chronotype_interval_beta"


se_coef_issp_2007_left_right_model_day <- data.frame(arm::se.coef(issp_2007_left_right_model_day)$country)
se_coef_issp_2007_left_right_model_day <- subset(se_coef_issp_2007_left_right_model_day, select = c("chronotype_interval_beta"))
se_coef_issp_2007_left_right_model_day$country <- row.names(se_coef_issp_2007_left_right_model_day)
names(se_coef_issp_2007_left_right_model_day)[names(se_coef_issp_2007_left_right_model_day)=="chronotype_interval_beta"] <- "se_chronotype_interval_beta"

coef_se_issp_2007_left_right_model_day <- merge(coef_issp_2007_left_right_model_day, se_coef_issp_2007_left_right_model_day, by="country")

coef_se_issp_2007_left_right_model_day$chrono.cl.90 <- coef_se_issp_2007_left_right_model_day$coef_chronotype_interval_beta - 1.645*coef_se_issp_2007_left_right_model_day$se_chronotype_interval_beta
coef_se_issp_2007_left_right_model_day$chrono.cu.90 <- coef_se_issp_2007_left_right_model_day$coef_chronotype_interval_beta + 1.645*coef_se_issp_2007_left_right_model_day$se_chronotype_interval_beta
coef_se_issp_2007_left_right_model_day$chrono.cl.95 <- coef_se_issp_2007_left_right_model_day$coef_chronotype_interval_beta - 1.96*coef_se_issp_2007_left_right_model_day$se_chronotype_interval_beta
coef_se_issp_2007_left_right_model_day$chrono.cu.95 <- coef_se_issp_2007_left_right_model_day$coef_chronotype_interval_beta + 1.96*coef_se_issp_2007_left_right_model_day$se_chronotype_interval_beta
coef_se_issp_2007_left_right_model_day$chrono.cl.99 <- coef_se_issp_2007_left_right_model_day$coef_chronotype_interval_beta - 2.576*coef_se_issp_2007_left_right_model_day$se_chronotype_interval_beta
coef_se_issp_2007_left_right_model_day$chrono.cu.99 <- coef_se_issp_2007_left_right_model_day$coef_chronotype_interval_beta + 2.576*coef_se_issp_2007_left_right_model_day$se_chronotype_interval_beta

View(coef_se_issp_2007_left_right_model_day)

ranef(issp_2007_left_right_model_day)$country
ranef_issp_2007_left_right_model_day <- data.frame(ranef(issp_2007_left_right_model_day)$country)
ranef_issp_2007_left_right_model_day <- subset(ranef_issp_2007_left_right_model_day, select = c("chronotype_interval_beta"))
ranef_issp_2007_left_right_model_day$country <- row.names(ranef_issp_2007_left_right_model_day)
names(ranef_issp_2007_left_right_model_day)[names(ranef_issp_2007_left_right_model_day)=="chronotype_interval_beta"] <- "ranef_chronotype_interval_beta"


se_ranef_issp_2007_left_right_model_day <- data.frame(arm::se.ranef(issp_2007_left_right_model_day)$country)
se_ranef_issp_2007_left_right_model_day <- subset(se_ranef_issp_2007_left_right_model_day, select = c("chronotype_interval_beta"))
se_ranef_issp_2007_left_right_model_day$country <- row.names(se_ranef_issp_2007_left_right_model_day)
names(se_ranef_issp_2007_left_right_model_day)[names(se_ranef_issp_2007_left_right_model_day)=="chronotype_interval_beta"] <- "se_chronotype_interval_beta"

ranef_se_issp_2007_left_right_model_day <- merge(ranef_issp_2007_left_right_model_day, se_ranef_issp_2007_left_right_model_day, by="country")

ranef_se_issp_2007_left_right_model_day$chrono.cl.90 <- ranef_se_issp_2007_left_right_model_day$ranef_chronotype_interval_beta - 1.645*ranef_se_issp_2007_left_right_model_day$se_chronotype_interval_beta
ranef_se_issp_2007_left_right_model_day$chrono.cu.90 <- ranef_se_issp_2007_left_right_model_day$ranef_chronotype_interval_beta + 1.645*ranef_se_issp_2007_left_right_model_day$se_chronotype_interval_beta
ranef_se_issp_2007_left_right_model_day$chrono.cl.95 <- ranef_se_issp_2007_left_right_model_day$ranef_chronotype_interval_beta - 1.96*ranef_se_issp_2007_left_right_model_day$se_chronotype_interval_beta
ranef_se_issp_2007_left_right_model_day$chrono.cu.95 <- ranef_se_issp_2007_left_right_model_day$ranef_chronotype_interval_beta + 1.96*ranef_se_issp_2007_left_right_model_day$se_chronotype_interval_beta
ranef_se_issp_2007_left_right_model_day$chrono.cl.99 <- ranef_se_issp_2007_left_right_model_day$ranef_chronotype_interval_beta - 2.576*ranef_se_issp_2007_left_right_model_day$se_chronotype_interval_beta
ranef_se_issp_2007_left_right_model_day$chrono.cu.99 <- ranef_se_issp_2007_left_right_model_day$ranef_chronotype_interval_beta + 2.576*ranef_se_issp_2007_left_right_model_day$se_chronotype_interval_beta

View(ranef_se_issp_2007_left_right_model_day)

coef(issp_2007_religiosity_model_day)
arm::se.coef(issp_2007_religiosity_model_day)

ranef(issp_2007_religiosity_model_day)
arm::se.ranef(issp_2007_religiosity_model_day)

coef(issp_2007_religiosity_model_day)$country
coef_issp_2007_religiosity_model_day <- data.frame(coef(issp_2007_religiosity_model_day)$country)
coef_issp_2007_religiosity_model_day <- subset(coef_issp_2007_religiosity_model_day, select = c("chronotype_interval_beta"))
coef_issp_2007_religiosity_model_day$country <- row.names(coef_issp_2007_religiosity_model_day)
names(coef_issp_2007_religiosity_model_day)[names(coef_issp_2007_religiosity_model_day)=="chronotype_interval_beta"] <- "coef_chronotype_interval_beta"


se_coef_issp_2007_religiosity_model_day <- data.frame(arm::se.coef(issp_2007_religiosity_model_day)$country)
se_coef_issp_2007_religiosity_model_day <- subset(se_coef_issp_2007_religiosity_model_day, select = c("chronotype_interval_beta"))
se_coef_issp_2007_religiosity_model_day$country <- row.names(se_coef_issp_2007_religiosity_model_day)
names(se_coef_issp_2007_religiosity_model_day)[names(se_coef_issp_2007_religiosity_model_day)=="chronotype_interval_beta"] <- "se_chronotype_interval_beta"

coef_se_issp_2007_religiosity_model_day <- merge(coef_issp_2007_religiosity_model_day, se_coef_issp_2007_religiosity_model_day, by="country")

coef_se_issp_2007_religiosity_model_day$chrono.cl.90 <- coef_se_issp_2007_religiosity_model_day$coef_chronotype_interval_beta - 1.645*coef_se_issp_2007_religiosity_model_day$se_chronotype_interval_beta
coef_se_issp_2007_religiosity_model_day$chrono.cu.90 <- coef_se_issp_2007_religiosity_model_day$coef_chronotype_interval_beta + 1.645*coef_se_issp_2007_religiosity_model_day$se_chronotype_interval_beta
coef_se_issp_2007_religiosity_model_day$chrono.cl.95 <- coef_se_issp_2007_religiosity_model_day$coef_chronotype_interval_beta - 1.96*coef_se_issp_2007_religiosity_model_day$se_chronotype_interval_beta
coef_se_issp_2007_religiosity_model_day$chrono.cu.95 <- coef_se_issp_2007_religiosity_model_day$coef_chronotype_interval_beta + 1.96*coef_se_issp_2007_religiosity_model_day$se_chronotype_interval_beta
coef_se_issp_2007_religiosity_model_day$chrono.cl.99 <- coef_se_issp_2007_religiosity_model_day$coef_chronotype_interval_beta - 2.576*coef_se_issp_2007_religiosity_model_day$se_chronotype_interval_beta
coef_se_issp_2007_religiosity_model_day$chrono.cu.99 <- coef_se_issp_2007_religiosity_model_day$coef_chronotype_interval_beta + 2.576*coef_se_issp_2007_religiosity_model_day$se_chronotype_interval_beta

View(coef_se_issp_2007_religiosity_model_day)

ranef(issp_2007_religiosity_model_day)$country
ranef_issp_2007_religiosity_model_day <- data.frame(ranef(issp_2007_religiosity_model_day)$country)
ranef_issp_2007_religiosity_model_day <- subset(ranef_issp_2007_religiosity_model_day, select = c("chronotype_interval_beta"))
ranef_issp_2007_religiosity_model_day$country <- row.names(ranef_issp_2007_religiosity_model_day)
names(ranef_issp_2007_religiosity_model_day)[names(ranef_issp_2007_religiosity_model_day)=="chronotype_interval_beta"] <- "ranef_chronotype_interval_beta"


se_ranef_issp_2007_religiosity_model_day <- data.frame(arm::se.ranef(issp_2007_religiosity_model_day)$country)
se_ranef_issp_2007_religiosity_model_day <- subset(se_ranef_issp_2007_religiosity_model_day, select = c("chronotype_interval_beta"))
se_ranef_issp_2007_religiosity_model_day$country <- row.names(se_ranef_issp_2007_religiosity_model_day)
names(se_ranef_issp_2007_religiosity_model_day)[names(se_ranef_issp_2007_religiosity_model_day)=="chronotype_interval_beta"] <- "se_chronotype_interval_beta"

ranef_se_issp_2007_religiosity_model_day <- merge(ranef_issp_2007_religiosity_model_day, se_ranef_issp_2007_religiosity_model_day, by="country")

ranef_se_issp_2007_religiosity_model_day$chrono.cl.90 <- ranef_se_issp_2007_religiosity_model_day$ranef_chronotype_interval_beta - 1.645*ranef_se_issp_2007_religiosity_model_day$se_chronotype_interval_beta
ranef_se_issp_2007_religiosity_model_day$chrono.cu.90 <- ranef_se_issp_2007_religiosity_model_day$ranef_chronotype_interval_beta + 1.645*ranef_se_issp_2007_religiosity_model_day$se_chronotype_interval_beta
ranef_se_issp_2007_religiosity_model_day$chrono.cl.95 <- ranef_se_issp_2007_religiosity_model_day$ranef_chronotype_interval_beta - 1.96*ranef_se_issp_2007_religiosity_model_day$se_chronotype_interval_beta
ranef_se_issp_2007_religiosity_model_day$chrono.cu.95 <- ranef_se_issp_2007_religiosity_model_day$ranef_chronotype_interval_beta + 1.96*ranef_se_issp_2007_religiosity_model_day$se_chronotype_interval_beta
ranef_se_issp_2007_religiosity_model_day$chrono.cl.99 <- ranef_se_issp_2007_religiosity_model_day$ranef_chronotype_interval_beta - 2.576*ranef_se_issp_2007_religiosity_model_day$se_chronotype_interval_beta
ranef_se_issp_2007_religiosity_model_day$chrono.cu.99 <- ranef_se_issp_2007_religiosity_model_day$ranef_chronotype_interval_beta + 2.576*ranef_se_issp_2007_religiosity_model_day$se_chronotype_interval_beta

View(ranef_se_issp_2007_religiosity_model_day)

#### Exploratory Analyses 2 ####

table(issp_2007$religious_denomination, exclude = NULL)

table(issp_2007$latitude, exclude = NULL)
issp_2007$latitude_avg <- mean(issp_2007$latitude, na.rm = T)
issp_2007$latitude_sd <- sd(issp_2007$latitude, na.rm = T)
issp_2007$latitude_2sd <- issp_2007$latitude_sd*2
issp_2007$latitude_beta <- (issp_2007$latitude-issp_2007$latitude_avg)/issp_2007$latitude_2sd

table(issp_2007$longitude, exclude = NULL)
issp_2007$longitude_avg <- mean(issp_2007$longitude, na.rm = T)
issp_2007$longitude_sd <- sd(issp_2007$longitude, na.rm = T)
issp_2007$longitude_2sd <- issp_2007$longitude_sd*2
issp_2007$longitude_beta <- (issp_2007$longitude-issp_2007$longitude_avg)/issp_2007$longitude_2sd

table(issp_2007$mean_temperature, exclude = NULL)
issp_2007$mean_temperature_avg <- mean(issp_2007$mean_temperature, na.rm = T)
issp_2007$mean_temperature_sd <- sd(issp_2007$mean_temperature, na.rm = T)
issp_2007$mean_temperature_2sd <- issp_2007$mean_temperature_sd*2
issp_2007$mean_temperature_beta <- (issp_2007$mean_temperature-issp_2007$mean_temperature_avg)/issp_2007$mean_temperature_2sd

issp_2007_left_right_model_geo <- lmer(PARTY_LR_beta ~ chronotype_interval_beta + latitude_beta + longitude_beta + mean_temperature_beta +  
                                         urban_rural_area_beta +
                                         sex_beta + age_beta + degree_beta + income_beta + religiosity_beta + political_interest_beta +
                                         religious_denomination_Buddhism_beta + 
                                         religious_denomination_Christian_Orthodox_beta +
                                         religious_denomination_Hinduism_beta +
                                         religious_denomination_Islam_beta +
                                         religious_denomination_Jewish_beta + 
                                         religious_denomination_No_religion_beta +
                                         religious_denomination_Other_Christian_Religions_beta +
                                         religious_denomination_Other_Eastern_Religions_beta +
                                         religious_denomination_Other_Religions_beta +
                                         religious_denomination_Protestant_beta + 
                                         (1 + chronotype_interval_beta | country), 
                                       data =  issp_2007)
summary(issp_2007_left_right_model_geo)
tab_model(issp_2007_left_right_model_geo, show.ci = 0.95)

issp_2007_religiosity_model_geo <- lmer(religiosity_beta ~ chronotype_interval_beta + latitude_beta + longitude_beta + mean_temperature_beta +  
                                          urban_rural_area_beta +
                                          sex_beta + age_beta + degree_beta + income_beta + PARTY_LR_beta + political_interest_beta +
                                          religious_denomination_Buddhism_beta + 
                                          religious_denomination_Christian_Orthodox_beta +
                                          religious_denomination_Hinduism_beta +
                                          religious_denomination_Islam_beta +
                                          religious_denomination_Jewish_beta + 
                                          religious_denomination_No_religion_beta +
                                          religious_denomination_Other_Christian_Religions_beta +
                                          religious_denomination_Other_Eastern_Religions_beta +
                                          religious_denomination_Other_Religions_beta +
                                          religious_denomination_Protestant_beta + (1 + chronotype_interval_beta | country), 
                                        data =  issp_2007)
summary(issp_2007_religiosity_model_geo)
tab_model(issp_2007_religiosity_model_geo, show.ci = 0.95)

#### Online Appendices, Table 6A: Multilevel linear modeling of left-right ideological placement and religious attendance additionally controlling for religious denomination and geographical covariates ####

stargazer(issp_2007_left_right_model_geo, issp_2007_religiosity_model_geo,
          type = "html", title=" ", digits=2, out="Tables/models_issp_2007_geo_denominations.htm",
          model.numbers = F,
          column.labels = c("Model 1", "Model 2"),
          covariate.labels = c("Chronotype", 
                               "Latitude",
                               "Longitude",
                               "Mean Temperature",
                               "Urban-Rural Area of Residence",
                               "Sex: Male (Base: Female)",
                               "Age",
                               "Education", 
                               "Income",
                               "Religious Attendance",
                               "Left-Right Ideological Placement",
                               "Level on Interest in Politics",
                               "Religious Denomination: Buddhism",
                               "Religious Denomination: Christian Orthodox",
                               "Religious Denomination: Hinduism",
                               "Religious Denomination: Islam",
                               "Religious Denomination: Jewish",
                               "Religious Denomination: No Religion",
                               "Religious Denomination: Other Christian Religions",
                               "Religious Denomination: Other Eastern Religions",
                               "Religious Denomination: Other Religions",
                               "Religious Denomination: Protestant",
                               "Intercept"))

tab_model(issp_2007_left_right_model_geo, show.ci = .95)
tab_model(issp_2007_religiosity_model_geo, show.ci = .95)

#### Preparation Online Appendices, Table 6B: Predicted and random effects of chronotype on left-right ideological placement and religious attendance for each country by additionally controlling for religious denomination and geographical covariates ####

coef(issp_2007_left_right_model_geo)
arm::se.coef(issp_2007_left_right_model_geo)

ranef(issp_2007_left_right_model_geo)
arm::se.ranef(issp_2007_left_right_model_geo)

coef(issp_2007_left_right_model_geo)$country
coef_issp_2007_left_right_model_geo <- data.frame(coef(issp_2007_left_right_model_geo)$country)
coef_issp_2007_left_right_model_geo <- subset(coef_issp_2007_left_right_model_geo, select = c("chronotype_interval_beta"))
coef_issp_2007_left_right_model_geo$country <- row.names(coef_issp_2007_left_right_model_geo)
names(coef_issp_2007_left_right_model_geo)[names(coef_issp_2007_left_right_model_geo)=="chronotype_interval_beta"] <- "coef_chronotype_interval_beta"


se_coef_issp_2007_left_right_model_geo <- data.frame(arm::se.coef(issp_2007_left_right_model_geo)$country)
se_coef_issp_2007_left_right_model_geo <- subset(se_coef_issp_2007_left_right_model_geo, select = c("chronotype_interval_beta"))
se_coef_issp_2007_left_right_model_geo$country <- row.names(se_coef_issp_2007_left_right_model_geo)
names(se_coef_issp_2007_left_right_model_geo)[names(se_coef_issp_2007_left_right_model_geo)=="chronotype_interval_beta"] <- "se_chronotype_interval_beta"

coef_se_issp_2007_left_right_model_geo <- merge(coef_issp_2007_left_right_model_geo, se_coef_issp_2007_left_right_model_geo, by="country")

coef_se_issp_2007_left_right_model_geo$chrono.cl.90 <- coef_se_issp_2007_left_right_model_geo$coef_chronotype_interval_beta - 1.645*coef_se_issp_2007_left_right_model_geo$se_chronotype_interval_beta
coef_se_issp_2007_left_right_model_geo$chrono.cu.90 <- coef_se_issp_2007_left_right_model_geo$coef_chronotype_interval_beta + 1.645*coef_se_issp_2007_left_right_model_geo$se_chronotype_interval_beta
coef_se_issp_2007_left_right_model_geo$chrono.cl.95 <- coef_se_issp_2007_left_right_model_geo$coef_chronotype_interval_beta - 1.96*coef_se_issp_2007_left_right_model_geo$se_chronotype_interval_beta
coef_se_issp_2007_left_right_model_geo$chrono.cu.95 <- coef_se_issp_2007_left_right_model_geo$coef_chronotype_interval_beta + 1.96*coef_se_issp_2007_left_right_model_geo$se_chronotype_interval_beta
coef_se_issp_2007_left_right_model_geo$chrono.cl.99 <- coef_se_issp_2007_left_right_model_geo$coef_chronotype_interval_beta - 2.576*coef_se_issp_2007_left_right_model_geo$se_chronotype_interval_beta
coef_se_issp_2007_left_right_model_geo$chrono.cu.99 <- coef_se_issp_2007_left_right_model_geo$coef_chronotype_interval_beta + 2.576*coef_se_issp_2007_left_right_model_geo$se_chronotype_interval_beta

View(coef_se_issp_2007_left_right_model_geo)

ranef(issp_2007_left_right_model_geo)$country
ranef_issp_2007_left_right_model_geo <- data.frame(ranef(issp_2007_left_right_model_geo)$country)
ranef_issp_2007_left_right_model_geo <- subset(ranef_issp_2007_left_right_model_geo, select = c("chronotype_interval_beta"))
ranef_issp_2007_left_right_model_geo$country <- row.names(ranef_issp_2007_left_right_model_geo)
names(ranef_issp_2007_left_right_model_geo)[names(ranef_issp_2007_left_right_model_geo)=="chronotype_interval_beta"] <- "ranef_chronotype_interval_beta"


se_ranef_issp_2007_left_right_model_geo <- data.frame(arm::se.ranef(issp_2007_left_right_model_geo)$country)
se_ranef_issp_2007_left_right_model_geo <- subset(se_ranef_issp_2007_left_right_model_geo, select = c("chronotype_interval_beta"))
se_ranef_issp_2007_left_right_model_geo$country <- row.names(se_ranef_issp_2007_left_right_model_geo)
names(se_ranef_issp_2007_left_right_model_geo)[names(se_ranef_issp_2007_left_right_model_geo)=="chronotype_interval_beta"] <- "se_chronotype_interval_beta"

ranef_se_issp_2007_left_right_model_geo <- merge(ranef_issp_2007_left_right_model_geo, se_ranef_issp_2007_left_right_model_geo, by="country")

ranef_se_issp_2007_left_right_model_geo$chrono.cl.90 <- ranef_se_issp_2007_left_right_model_geo$ranef_chronotype_interval_beta - 1.645*ranef_se_issp_2007_left_right_model_geo$se_chronotype_interval_beta
ranef_se_issp_2007_left_right_model_geo$chrono.cu.90 <- ranef_se_issp_2007_left_right_model_geo$ranef_chronotype_interval_beta + 1.645*ranef_se_issp_2007_left_right_model_geo$se_chronotype_interval_beta
ranef_se_issp_2007_left_right_model_geo$chrono.cl.95 <- ranef_se_issp_2007_left_right_model_geo$ranef_chronotype_interval_beta - 1.96*ranef_se_issp_2007_left_right_model_geo$se_chronotype_interval_beta
ranef_se_issp_2007_left_right_model_geo$chrono.cu.95 <- ranef_se_issp_2007_left_right_model_geo$ranef_chronotype_interval_beta + 1.96*ranef_se_issp_2007_left_right_model_geo$se_chronotype_interval_beta
ranef_se_issp_2007_left_right_model_geo$chrono.cl.99 <- ranef_se_issp_2007_left_right_model_geo$ranef_chronotype_interval_beta - 2.576*ranef_se_issp_2007_left_right_model_geo$se_chronotype_interval_beta
ranef_se_issp_2007_left_right_model_geo$chrono.cu.99 <- ranef_se_issp_2007_left_right_model_geo$ranef_chronotype_interval_beta + 2.576*ranef_se_issp_2007_left_right_model_geo$se_chronotype_interval_beta

View(ranef_se_issp_2007_left_right_model_geo)

coef(issp_2007_religiosity_model_geo)
arm::se.coef(issp_2007_religiosity_model_geo)

ranef(issp_2007_religiosity_model_geo)
arm::se.ranef(issp_2007_religiosity_model_geo)

coef(issp_2007_religiosity_model_geo)$country
coef_issp_2007_religiosity_model_geo <- data.frame(coef(issp_2007_religiosity_model_geo)$country)
coef_issp_2007_religiosity_model_geo <- subset(coef_issp_2007_religiosity_model_geo, select = c("chronotype_interval_beta"))
coef_issp_2007_religiosity_model_geo$country <- row.names(coef_issp_2007_religiosity_model_geo)
names(coef_issp_2007_religiosity_model_geo)[names(coef_issp_2007_religiosity_model_geo)=="chronotype_interval_beta"] <- "coef_chronotype_interval_beta"


se_coef_issp_2007_religiosity_model_geo <- data.frame(arm::se.coef(issp_2007_religiosity_model_geo)$country)
se_coef_issp_2007_religiosity_model_geo <- subset(se_coef_issp_2007_religiosity_model_geo, select = c("chronotype_interval_beta"))
se_coef_issp_2007_religiosity_model_geo$country <- row.names(se_coef_issp_2007_religiosity_model_geo)
names(se_coef_issp_2007_religiosity_model_geo)[names(se_coef_issp_2007_religiosity_model_geo)=="chronotype_interval_beta"] <- "se_chronotype_interval_beta"

coef_se_issp_2007_religiosity_model_geo <- merge(coef_issp_2007_religiosity_model_geo, se_coef_issp_2007_religiosity_model_geo, by="country")

coef_se_issp_2007_religiosity_model_geo$chrono.cl.90 <- coef_se_issp_2007_religiosity_model_geo$coef_chronotype_interval_beta - 1.645*coef_se_issp_2007_religiosity_model_geo$se_chronotype_interval_beta
coef_se_issp_2007_religiosity_model_geo$chrono.cu.90 <- coef_se_issp_2007_religiosity_model_geo$coef_chronotype_interval_beta + 1.645*coef_se_issp_2007_religiosity_model_geo$se_chronotype_interval_beta
coef_se_issp_2007_religiosity_model_geo$chrono.cl.95 <- coef_se_issp_2007_religiosity_model_geo$coef_chronotype_interval_beta - 1.96*coef_se_issp_2007_religiosity_model_geo$se_chronotype_interval_beta
coef_se_issp_2007_religiosity_model_geo$chrono.cu.95 <- coef_se_issp_2007_religiosity_model_geo$coef_chronotype_interval_beta + 1.96*coef_se_issp_2007_religiosity_model_geo$se_chronotype_interval_beta
coef_se_issp_2007_religiosity_model_geo$chrono.cl.99 <- coef_se_issp_2007_religiosity_model_geo$coef_chronotype_interval_beta - 2.576*coef_se_issp_2007_religiosity_model_geo$se_chronotype_interval_beta
coef_se_issp_2007_religiosity_model_geo$chrono.cu.99 <- coef_se_issp_2007_religiosity_model_geo$coef_chronotype_interval_beta + 2.576*coef_se_issp_2007_religiosity_model_geo$se_chronotype_interval_beta

View(coef_se_issp_2007_religiosity_model_geo)

ranef(issp_2007_religiosity_model_geo)$country
ranef_issp_2007_religiosity_model_geo <- data.frame(ranef(issp_2007_religiosity_model_geo)$country)
ranef_issp_2007_religiosity_model_geo <- subset(ranef_issp_2007_religiosity_model_geo, select = c("chronotype_interval_beta"))
ranef_issp_2007_religiosity_model_geo$country <- row.names(ranef_issp_2007_religiosity_model_geo)
names(ranef_issp_2007_religiosity_model_geo)[names(ranef_issp_2007_religiosity_model_geo)=="chronotype_interval_beta"] <- "ranef_chronotype_interval_beta"


se_ranef_issp_2007_religiosity_model_geo <- data.frame(arm::se.ranef(issp_2007_religiosity_model_geo)$country)
se_ranef_issp_2007_religiosity_model_geo <- subset(se_ranef_issp_2007_religiosity_model_geo, select = c("chronotype_interval_beta"))
se_ranef_issp_2007_religiosity_model_geo$country <- row.names(se_ranef_issp_2007_religiosity_model_geo)
names(se_ranef_issp_2007_religiosity_model_geo)[names(se_ranef_issp_2007_religiosity_model_geo)=="chronotype_interval_beta"] <- "se_chronotype_interval_beta"

ranef_se_issp_2007_religiosity_model_geo <- merge(ranef_issp_2007_religiosity_model_geo, se_ranef_issp_2007_religiosity_model_geo, by="country")

ranef_se_issp_2007_religiosity_model_geo$chrono.cl.90 <- ranef_se_issp_2007_religiosity_model_geo$ranef_chronotype_interval_beta - 1.645*ranef_se_issp_2007_religiosity_model_geo$se_chronotype_interval_beta
ranef_se_issp_2007_religiosity_model_geo$chrono.cu.90 <- ranef_se_issp_2007_religiosity_model_geo$ranef_chronotype_interval_beta + 1.645*ranef_se_issp_2007_religiosity_model_geo$se_chronotype_interval_beta
ranef_se_issp_2007_religiosity_model_geo$chrono.cl.95 <- ranef_se_issp_2007_religiosity_model_geo$ranef_chronotype_interval_beta - 1.96*ranef_se_issp_2007_religiosity_model_geo$se_chronotype_interval_beta
ranef_se_issp_2007_religiosity_model_geo$chrono.cu.95 <- ranef_se_issp_2007_religiosity_model_geo$ranef_chronotype_interval_beta + 1.96*ranef_se_issp_2007_religiosity_model_geo$se_chronotype_interval_beta
ranef_se_issp_2007_religiosity_model_geo$chrono.cl.99 <- ranef_se_issp_2007_religiosity_model_geo$ranef_chronotype_interval_beta - 2.576*ranef_se_issp_2007_religiosity_model_geo$se_chronotype_interval_beta
ranef_se_issp_2007_religiosity_model_geo$chrono.cu.99 <- ranef_se_issp_2007_religiosity_model_geo$ranef_chronotype_interval_beta + 2.576*ranef_se_issp_2007_religiosity_model_geo$se_chronotype_interval_beta

View(ranef_se_issp_2007_religiosity_model_geo)

#### Addition ####

issp_2007_religiosity_model_geo_m1 <- lmer(religiosity_beta ~ chronotype_interval_beta + latitude_beta + longitude_beta + mean_temperature_beta +  
                                             urban_rural_area_beta +
                                             sex_beta + age_beta + degree_beta + income_beta + PARTY_LR_beta + political_interest_beta +
                                             (1 + chronotype_interval_beta | country), 
                                           data =  issp_2007)
summary(issp_2007_religiosity_model_geo_m1)
tab_model(issp_2007_religiosity_model_geo_m1, show.ci = 0.95)

issp_2007_religiosity_model_geo_m2 <- lmer(religiosity_beta ~ chronotype_interval_beta + 
                                             urban_rural_area_beta +
                                             sex_beta + age_beta + degree_beta + income_beta + PARTY_LR_beta + political_interest_beta +
                                             religious_denomination_Buddhism_beta + 
                                             religious_denomination_Christian_Orthodox_beta +
                                             religious_denomination_Hinduism_beta +
                                             religious_denomination_Islam_beta +
                                             religious_denomination_Jewish_beta + 
                                             religious_denomination_No_religion_beta +
                                             religious_denomination_Other_Christian_Religions_beta +
                                             religious_denomination_Other_Eastern_Religions_beta +
                                             religious_denomination_Other_Religions_beta +
                                             religious_denomination_Protestant_beta + (1 + chronotype_interval_beta | country), 
                                           data =  issp_2007)
summary(issp_2007_religiosity_model_geo_m2)
tab_model(issp_2007_religiosity_model_geo_m2, show.ci = 0.95)

#### Online Appendices, Table 6C: Stepwise multilevel linear modeling of religious attendance additionally controlling for religious denomination and geographical covariates ####

stargazer(issp_2007_religiosity_model_geo_m1, issp_2007_religiosity_model_geo_m2,
          type = "html", title=" ", digits=2, out="Tables/models_issp_2007_geo_denominations_stepwise.htm",
          model.numbers = F,
          column.labels = c("Model 1", "Model 2"),
          covariate.labels = c("Chronotype", 
                               "Latitude",
                               "Longitude",
                               "Mean Temperature",
                               "Urban-Rural Area of Residence",
                               "Sex: Male (Base: Female)",
                               "Age",
                               "Education", 
                               "Income",
                               "Left-Right Ideological Placement",
                               "Level on Interest in Politics",
                               "Religious Denomination: Buddhism",
                               "Religious Denomination: Christian Orthodox",
                               "Religious Denomination: Hinduism",
                               "Religious Denomination: Islam",
                               "Religious Denomination: Jewish",
                               "Religious Denomination: No Religion",
                               "Religious Denomination: Other Christian Religions",
                               "Religious Denomination: Other Eastern Religions",
                               "Religious Denomination: Other Religions",
                               "Religious Denomination: Protestant",
                               "Intercept"))

tab_model(issp_2007_religiosity_model_geo_m1, show.ci = .95)
tab_model(issp_2007_religiosity_model_geo_m2, show.ci = .95)

#### Preparation Online Appendices, Table 6D: Predicted and random effects of chronotype on religious attendance for each country by additionally controlling for religious denomination and geographical covariates in stepwise modeling ####

coef(issp_2007_religiosity_model_geo_m1)
arm::se.coef(issp_2007_religiosity_model_geo_m1)

ranef(issp_2007_religiosity_model_geo_m1)
arm::se.ranef(issp_2007_religiosity_model_geo_m1)

coef(issp_2007_religiosity_model_geo_m1)$country
coef_issp_2007_religiosity_model_geo_m1 <- data.frame(coef(issp_2007_religiosity_model_geo_m1)$country)
coef_issp_2007_religiosity_model_geo_m1 <- subset(coef_issp_2007_religiosity_model_geo_m1, select = c("chronotype_interval_beta"))
coef_issp_2007_religiosity_model_geo_m1$country <- row.names(coef_issp_2007_religiosity_model_geo_m1)
names(coef_issp_2007_religiosity_model_geo_m1)[names(coef_issp_2007_religiosity_model_geo_m1)=="chronotype_interval_beta"] <- "coef_chronotype_interval_beta"


se_coef_issp_2007_religiosity_model_geo_m1 <- data.frame(arm::se.coef(issp_2007_religiosity_model_geo_m1)$country)
se_coef_issp_2007_religiosity_model_geo_m1 <- subset(se_coef_issp_2007_religiosity_model_geo_m1, select = c("chronotype_interval_beta"))
se_coef_issp_2007_religiosity_model_geo_m1$country <- row.names(se_coef_issp_2007_religiosity_model_geo_m1)
names(se_coef_issp_2007_religiosity_model_geo_m1)[names(se_coef_issp_2007_religiosity_model_geo_m1)=="chronotype_interval_beta"] <- "se_chronotype_interval_beta"

coef_se_issp_2007_religiosity_model_geo_m1 <- merge(coef_issp_2007_religiosity_model_geo_m1, se_coef_issp_2007_religiosity_model_geo_m1, by="country")

coef_se_issp_2007_religiosity_model_geo_m1$chrono.cl.90 <- coef_se_issp_2007_religiosity_model_geo_m1$coef_chronotype_interval_beta - 1.645*coef_se_issp_2007_religiosity_model_geo_m1$se_chronotype_interval_beta
coef_se_issp_2007_religiosity_model_geo_m1$chrono.cu.90 <- coef_se_issp_2007_religiosity_model_geo_m1$coef_chronotype_interval_beta + 1.645*coef_se_issp_2007_religiosity_model_geo_m1$se_chronotype_interval_beta
coef_se_issp_2007_religiosity_model_geo_m1$chrono.cl.95 <- coef_se_issp_2007_religiosity_model_geo_m1$coef_chronotype_interval_beta - 1.96*coef_se_issp_2007_religiosity_model_geo_m1$se_chronotype_interval_beta
coef_se_issp_2007_religiosity_model_geo_m1$chrono.cu.95 <- coef_se_issp_2007_religiosity_model_geo_m1$coef_chronotype_interval_beta + 1.96*coef_se_issp_2007_religiosity_model_geo_m1$se_chronotype_interval_beta
coef_se_issp_2007_religiosity_model_geo_m1$chrono.cl.99 <- coef_se_issp_2007_religiosity_model_geo_m1$coef_chronotype_interval_beta - 2.576*coef_se_issp_2007_religiosity_model_geo_m1$se_chronotype_interval_beta
coef_se_issp_2007_religiosity_model_geo_m1$chrono.cu.99 <- coef_se_issp_2007_religiosity_model_geo_m1$coef_chronotype_interval_beta + 2.576*coef_se_issp_2007_religiosity_model_geo_m1$se_chronotype_interval_beta

View(coef_se_issp_2007_religiosity_model_geo_m1)

ranef(issp_2007_religiosity_model_geo_m1)$country
ranef_issp_2007_religiosity_model_geo_m1 <- data.frame(ranef(issp_2007_religiosity_model_geo_m1)$country)
ranef_issp_2007_religiosity_model_geo_m1 <- subset(ranef_issp_2007_religiosity_model_geo_m1, select = c("chronotype_interval_beta"))
ranef_issp_2007_religiosity_model_geo_m1$country <- row.names(ranef_issp_2007_religiosity_model_geo_m1)
names(ranef_issp_2007_religiosity_model_geo_m1)[names(ranef_issp_2007_religiosity_model_geo_m1)=="chronotype_interval_beta"] <- "ranef_chronotype_interval_beta"


se_ranef_issp_2007_religiosity_model_geo_m1 <- data.frame(arm::se.ranef(issp_2007_religiosity_model_geo_m1)$country)
se_ranef_issp_2007_religiosity_model_geo_m1 <- subset(se_ranef_issp_2007_religiosity_model_geo_m1, select = c("chronotype_interval_beta"))
se_ranef_issp_2007_religiosity_model_geo_m1$country <- row.names(se_ranef_issp_2007_religiosity_model_geo_m1)
names(se_ranef_issp_2007_religiosity_model_geo_m1)[names(se_ranef_issp_2007_religiosity_model_geo_m1)=="chronotype_interval_beta"] <- "se_chronotype_interval_beta"

ranef_se_issp_2007_religiosity_model_geo_m1 <- merge(ranef_issp_2007_religiosity_model_geo_m1, se_ranef_issp_2007_religiosity_model_geo_m1, by="country")

ranef_se_issp_2007_religiosity_model_geo_m1$chrono.cl.90 <- ranef_se_issp_2007_religiosity_model_geo_m1$ranef_chronotype_interval_beta - 1.645*ranef_se_issp_2007_religiosity_model_geo_m1$se_chronotype_interval_beta
ranef_se_issp_2007_religiosity_model_geo_m1$chrono.cu.90 <- ranef_se_issp_2007_religiosity_model_geo_m1$ranef_chronotype_interval_beta + 1.645*ranef_se_issp_2007_religiosity_model_geo_m1$se_chronotype_interval_beta
ranef_se_issp_2007_religiosity_model_geo_m1$chrono.cl.95 <- ranef_se_issp_2007_religiosity_model_geo_m1$ranef_chronotype_interval_beta - 1.96*ranef_se_issp_2007_religiosity_model_geo_m1$se_chronotype_interval_beta
ranef_se_issp_2007_religiosity_model_geo_m1$chrono.cu.95 <- ranef_se_issp_2007_religiosity_model_geo_m1$ranef_chronotype_interval_beta + 1.96*ranef_se_issp_2007_religiosity_model_geo_m1$se_chronotype_interval_beta
ranef_se_issp_2007_religiosity_model_geo_m1$chrono.cl.99 <- ranef_se_issp_2007_religiosity_model_geo_m1$ranef_chronotype_interval_beta - 2.576*ranef_se_issp_2007_religiosity_model_geo_m1$se_chronotype_interval_beta
ranef_se_issp_2007_religiosity_model_geo_m1$chrono.cu.99 <- ranef_se_issp_2007_religiosity_model_geo_m1$ranef_chronotype_interval_beta + 2.576*ranef_se_issp_2007_religiosity_model_geo_m1$se_chronotype_interval_beta

View(ranef_se_issp_2007_religiosity_model_geo_m1)

coef(issp_2007_religiosity_model_geo_m2)
arm::se.coef(issp_2007_religiosity_model_geo_m2)

ranef(issp_2007_religiosity_model_geo_m2)
arm::se.ranef(issp_2007_religiosity_model_geo_m2)

coef(issp_2007_religiosity_model_geo_m2)$country
coef_issp_2007_religiosity_model_geo_m2 <- data.frame(coef(issp_2007_religiosity_model_geo_m2)$country)
coef_issp_2007_religiosity_model_geo_m2 <- subset(coef_issp_2007_religiosity_model_geo_m2, select = c("chronotype_interval_beta"))
coef_issp_2007_religiosity_model_geo_m2$country <- row.names(coef_issp_2007_religiosity_model_geo_m2)
names(coef_issp_2007_religiosity_model_geo_m2)[names(coef_issp_2007_religiosity_model_geo_m2)=="chronotype_interval_beta"] <- "coef_chronotype_interval_beta"


se_coef_issp_2007_religiosity_model_geo_m2 <- data.frame(arm::se.coef(issp_2007_religiosity_model_geo_m2)$country)
se_coef_issp_2007_religiosity_model_geo_m2 <- subset(se_coef_issp_2007_religiosity_model_geo_m2, select = c("chronotype_interval_beta"))
se_coef_issp_2007_religiosity_model_geo_m2$country <- row.names(se_coef_issp_2007_religiosity_model_geo_m2)
names(se_coef_issp_2007_religiosity_model_geo_m2)[names(se_coef_issp_2007_religiosity_model_geo_m2)=="chronotype_interval_beta"] <- "se_chronotype_interval_beta"

coef_se_issp_2007_religiosity_model_geo_m2 <- merge(coef_issp_2007_religiosity_model_geo_m2, se_coef_issp_2007_religiosity_model_geo_m2, by="country")

coef_se_issp_2007_religiosity_model_geo_m2$chrono.cl.90 <- coef_se_issp_2007_religiosity_model_geo_m2$coef_chronotype_interval_beta - 1.645*coef_se_issp_2007_religiosity_model_geo_m2$se_chronotype_interval_beta
coef_se_issp_2007_religiosity_model_geo_m2$chrono.cu.90 <- coef_se_issp_2007_religiosity_model_geo_m2$coef_chronotype_interval_beta + 1.645*coef_se_issp_2007_religiosity_model_geo_m2$se_chronotype_interval_beta
coef_se_issp_2007_religiosity_model_geo_m2$chrono.cl.95 <- coef_se_issp_2007_religiosity_model_geo_m2$coef_chronotype_interval_beta - 1.96*coef_se_issp_2007_religiosity_model_geo_m2$se_chronotype_interval_beta
coef_se_issp_2007_religiosity_model_geo_m2$chrono.cu.95 <- coef_se_issp_2007_religiosity_model_geo_m2$coef_chronotype_interval_beta + 1.96*coef_se_issp_2007_religiosity_model_geo_m2$se_chronotype_interval_beta
coef_se_issp_2007_religiosity_model_geo_m2$chrono.cl.99 <- coef_se_issp_2007_religiosity_model_geo_m2$coef_chronotype_interval_beta - 2.576*coef_se_issp_2007_religiosity_model_geo_m2$se_chronotype_interval_beta
coef_se_issp_2007_religiosity_model_geo_m2$chrono.cu.99 <- coef_se_issp_2007_religiosity_model_geo_m2$coef_chronotype_interval_beta + 2.576*coef_se_issp_2007_religiosity_model_geo_m2$se_chronotype_interval_beta

View(coef_se_issp_2007_religiosity_model_geo_m2)

ranef(issp_2007_religiosity_model_geo_m2)$country
ranef_issp_2007_religiosity_model_geo_m2 <- data.frame(ranef(issp_2007_religiosity_model_geo_m2)$country)
ranef_issp_2007_religiosity_model_geo_m2 <- subset(ranef_issp_2007_religiosity_model_geo_m2, select = c("chronotype_interval_beta"))
ranef_issp_2007_religiosity_model_geo_m2$country <- row.names(ranef_issp_2007_religiosity_model_geo_m2)
names(ranef_issp_2007_religiosity_model_geo_m2)[names(ranef_issp_2007_religiosity_model_geo_m2)=="chronotype_interval_beta"] <- "ranef_chronotype_interval_beta"


se_ranef_issp_2007_religiosity_model_geo_m2 <- data.frame(arm::se.ranef(issp_2007_religiosity_model_geo_m2)$country)
se_ranef_issp_2007_religiosity_model_geo_m2 <- subset(se_ranef_issp_2007_religiosity_model_geo_m2, select = c("chronotype_interval_beta"))
se_ranef_issp_2007_religiosity_model_geo_m2$country <- row.names(se_ranef_issp_2007_religiosity_model_geo_m2)
names(se_ranef_issp_2007_religiosity_model_geo_m2)[names(se_ranef_issp_2007_religiosity_model_geo_m2)=="chronotype_interval_beta"] <- "se_chronotype_interval_beta"

ranef_se_issp_2007_religiosity_model_geo_m2 <- merge(ranef_issp_2007_religiosity_model_geo_m2, se_ranef_issp_2007_religiosity_model_geo_m2, by="country")

ranef_se_issp_2007_religiosity_model_geo_m2$chrono.cl.90 <- ranef_se_issp_2007_religiosity_model_geo_m2$ranef_chronotype_interval_beta - 1.645*ranef_se_issp_2007_religiosity_model_geo_m2$se_chronotype_interval_beta
ranef_se_issp_2007_religiosity_model_geo_m2$chrono.cu.90 <- ranef_se_issp_2007_religiosity_model_geo_m2$ranef_chronotype_interval_beta + 1.645*ranef_se_issp_2007_religiosity_model_geo_m2$se_chronotype_interval_beta
ranef_se_issp_2007_religiosity_model_geo_m2$chrono.cl.95 <- ranef_se_issp_2007_religiosity_model_geo_m2$ranef_chronotype_interval_beta - 1.96*ranef_se_issp_2007_religiosity_model_geo_m2$se_chronotype_interval_beta
ranef_se_issp_2007_religiosity_model_geo_m2$chrono.cu.95 <- ranef_se_issp_2007_religiosity_model_geo_m2$ranef_chronotype_interval_beta + 1.96*ranef_se_issp_2007_religiosity_model_geo_m2$se_chronotype_interval_beta
ranef_se_issp_2007_religiosity_model_geo_m2$chrono.cl.99 <- ranef_se_issp_2007_religiosity_model_geo_m2$ranef_chronotype_interval_beta - 2.576*ranef_se_issp_2007_religiosity_model_geo_m2$se_chronotype_interval_beta
ranef_se_issp_2007_religiosity_model_geo_m2$chrono.cu.99 <- ranef_se_issp_2007_religiosity_model_geo_m2$ranef_chronotype_interval_beta + 2.576*ranef_se_issp_2007_religiosity_model_geo_m2$se_chronotype_interval_beta

View(ranef_se_issp_2007_religiosity_model_geo_m2)

#### Exploratory Analyses 3 ####

russia <- subset(read_dta("DATASETS/ZA4850_v2-0-0.dta"), subset = V5==643, select = c("PARTY_LR", "RU_PRTY"))

table(russia$PARTY_LR, exclude = NULL)

russia$PARTY_LR <- ifelse(russia$PARTY_LR==6, NA, russia$PARTY_LR)

russia$self_placement <- NA
russia$self_placement[russia$PARTY_LR==1] <- "Far-left"
russia$self_placement[russia$PARTY_LR==2] <- "Left, center left"
russia$self_placement[russia$PARTY_LR==3] <- "Center, liberal"
russia$self_placement[russia$PARTY_LR==4] <- "Right, conservative"
russia$self_placement[russia$PARTY_LR==5] <- "Far-right"
russia$self_placement[russia$PARTY_LR==6 | is.na(russia$PARTY_LR)] <- "Missing"
table(russia$self_placement, exclude = NULL)
russia$self_placement <- factor(russia$self_placement, levels = c("Far-left", "Left, center left", "Center, liberal", "Right, conservative", "Far-right",  "Missing"), ordered = F)
table(russia$self_placement, exclude = NULL)

table(russia$RU_PRTY, exclude = NULL)
russia$affiliated_party <- NA
russia$affiliated_party[russia$RU_PRTY==1] <- "Pensioners Party / Party of Social Justice"
russia$affiliated_party[russia$RU_PRTY==2] <- "Union of Right Forces"
russia$affiliated_party[russia$RU_PRTY==3] <- "Yabloko"
russia$affiliated_party[russia$RU_PRTY==4] <- "Russian Ecological Party"
russia$affiliated_party[russia$RU_PRTY==5] <- "Country Party of Russia"
russia$affiliated_party[russia$RU_PRTY==7] <- "Party of Revival / Russian Party of Life"
russia$affiliated_party[russia$RU_PRTY==8] <- "Motherland"
russia$affiliated_party[russia$RU_PRTY==9] <- "Liberal Democratic Party of Russia"
russia$affiliated_party[russia$RU_PRTY==10] <- "United Russia"
russia$affiliated_party[russia$RU_PRTY==11] <- "Communist Party of Russian Federation"
russia$affiliated_party[russia$RU_PRTY==95 | russia$RU_PRTY==96 | is.na(russia$RU_PRTY)] <- "Missing"
table(russia$affiliated_party, exclude = NULL)

table(russia$affiliated_party, russia$self_placement, exclude = NULL)

russia$affiliated_party_lr_evs <- NA
russia$affiliated_party_lr_evs[russia$affiliated_party=="Pensioners Party / Party of Social Justice"] <- 2
russia$affiliated_party_lr_evs[russia$affiliated_party=="Union of Right Forces"] <- 4
russia$affiliated_party_lr_evs[russia$affiliated_party=="Yabloko"] <- 3
russia$affiliated_party_lr_evs[russia$affiliated_party=="Country Party of Russia"] <- 1
russia$affiliated_party_lr_evs[russia$affiliated_party=="Party of Revival / Russian Party of Life"] <- 2
russia$affiliated_party_lr_evs[russia$affiliated_party=="Motherland"] <- 3
russia$affiliated_party_lr_evs[russia$affiliated_party=="Liberal Democratic Party of Russia"] <- 5
russia$affiliated_party_lr_evs[russia$affiliated_party=="United Russia"] <- 3
russia$affiliated_party_lr_evs[russia$affiliated_party=="Communist Party of Russian Federation"] <- 1
table(russia$affiliated_party_lr_evs, exclude = NULL)

cor.test(russia$PARTY_LR, russia$affiliated_party_lr_evs)
table(russia$affiliated_party_lr_evs, russia$PARTY_LR, exclude = NULL)

russia$affiliated_party_lr_cses <- NA
russia$affiliated_party_lr_cses[russia$affiliated_party=="Pensioners Party / Party of Social Justice"] <- 2
russia$affiliated_party_lr_cses[russia$affiliated_party=="Union of Right Forces"] <- 4
russia$affiliated_party_lr_cses[russia$affiliated_party=="Yabloko"] <- 3
russia$affiliated_party_lr_cses[russia$affiliated_party=="Country Party of Russia"] <- 2
russia$affiliated_party_lr_cses[russia$affiliated_party=="Party of Revival / Russian Party of Life"] <- 2
russia$affiliated_party_lr_cses[russia$affiliated_party=="Motherland"] <- 2
russia$affiliated_party_lr_cses[russia$affiliated_party=="Liberal Democratic Party of Russia"] <- 3
russia$affiliated_party_lr_cses[russia$affiliated_party=="United Russia"] <- 4
russia$affiliated_party_lr_cses[russia$affiliated_party=="Communist Party of Russian Federation"] <- 1
table(russia$affiliated_party_lr_cses, exclude = NULL)

cor.test(russia$PARTY_LR, russia$affiliated_party_lr_cses)
table(russia$affiliated_party_lr_cses, russia$PARTY_LR, exclude = NULL)

cor.test(russia$affiliated_party_lr_evs, russia$affiliated_party_lr_cses)

issp_2007$PARTY_LR_EVS_RU <- issp_2007$PARTY_LR
issp_2007$PARTY_LR_EVS_RU <- ifelse(issp_2007$country=="Russia" & issp_2007$RU_PRTY==1, 2, issp_2007$PARTY_LR_EVS_RU)
issp_2007$PARTY_LR_EVS_RU <- ifelse(issp_2007$country=="Russia" & issp_2007$RU_PRTY==2, 4, issp_2007$PARTY_LR_EVS_RU)
issp_2007$PARTY_LR_EVS_RU <- ifelse(issp_2007$country=="Russia" & issp_2007$RU_PRTY==3, 3, issp_2007$PARTY_LR_EVS_RU)
issp_2007$PARTY_LR_EVS_RU <- ifelse(issp_2007$country=="Russia" & issp_2007$RU_PRTY==5, 1, issp_2007$PARTY_LR_EVS_RU)
issp_2007$PARTY_LR_EVS_RU <- ifelse(issp_2007$country=="Russia" & issp_2007$RU_PRTY==7, 2, issp_2007$PARTY_LR_EVS_RU)
issp_2007$PARTY_LR_EVS_RU <- ifelse(issp_2007$country=="Russia" & issp_2007$RU_PRTY==8, 3, issp_2007$PARTY_LR_EVS_RU)
issp_2007$PARTY_LR_EVS_RU <- ifelse(issp_2007$country=="Russia" & issp_2007$RU_PRTY==9, 5, issp_2007$PARTY_LR_EVS_RU)
issp_2007$PARTY_LR_EVS_RU <- ifelse(issp_2007$country=="Russia" & issp_2007$RU_PRTY==10, 3, issp_2007$PARTY_LR_EVS_RU)
issp_2007$PARTY_LR_EVS_RU <- ifelse(issp_2007$country=="Russia" & issp_2007$RU_PRTY==11, 1, issp_2007$PARTY_LR_EVS_RU)
table(issp_2007$PARTY_LR_EVS_RU, issp_2007$PARTY_LR, exclude = NULL)

issp_2007$PARTY_LR_EVS_RU_avg <- mean(issp_2007$PARTY_LR_EVS_RU, na.rm = T)
issp_2007$PARTY_LR_EVS_RU_sd <- sd(issp_2007$PARTY_LR_EVS_RU, na.rm = T)
issp_2007$PARTY_LR_EVS_RU_2sd <- issp_2007$PARTY_LR_EVS_RU_sd*2
issp_2007$PARTY_LR_EVS_RU_beta <- (issp_2007$PARTY_LR_EVS_RU-issp_2007$PARTY_LR_EVS_RU_avg)/issp_2007$PARTY_LR_EVS_RU_2sd
table(issp_2007$PARTY_LR_EVS_RU_beta, exclude = NULL)

issp_2007$PARTY_LR_CSES_RU <- issp_2007$PARTY_LR
issp_2007$PARTY_LR_CSES_RU <- ifelse(issp_2007$country=="Russia" & issp_2007$RU_PRTY==1, 2, issp_2007$PARTY_LR_CSES_RU)
issp_2007$PARTY_LR_CSES_RU <- ifelse(issp_2007$country=="Russia" & issp_2007$RU_PRTY==2, 4, issp_2007$PARTY_LR_CSES_RU)
issp_2007$PARTY_LR_CSES_RU <- ifelse(issp_2007$country=="Russia" & issp_2007$RU_PRTY==3, 3, issp_2007$PARTY_LR_CSES_RU)
issp_2007$PARTY_LR_CSES_RU <- ifelse(issp_2007$country=="Russia" & issp_2007$RU_PRTY==5, 2, issp_2007$PARTY_LR_CSES_RU)
issp_2007$PARTY_LR_CSES_RU <- ifelse(issp_2007$country=="Russia" & issp_2007$RU_PRTY==7, 2, issp_2007$PARTY_LR_CSES_RU)
issp_2007$PARTY_LR_CSES_RU <- ifelse(issp_2007$country=="Russia" & issp_2007$RU_PRTY==8, 2, issp_2007$PARTY_LR_CSES_RU)
issp_2007$PARTY_LR_CSES_RU <- ifelse(issp_2007$country=="Russia" & issp_2007$RU_PRTY==9, 3, issp_2007$PARTY_LR_CSES_RU)
issp_2007$PARTY_LR_CSES_RU <- ifelse(issp_2007$country=="Russia" & issp_2007$RU_PRTY==10, 4, issp_2007$PARTY_LR_CSES_RU)
issp_2007$PARTY_LR_CSES_RU <- ifelse(issp_2007$country=="Russia" & issp_2007$RU_PRTY==11, 1, issp_2007$PARTY_LR_CSES_RU)
table(issp_2007$PARTY_LR_CSES_RU, issp_2007$PARTY_LR, exclude = NULL)

issp_2007$PARTY_LR_CSES_RU_avg <- mean(issp_2007$PARTY_LR_CSES_RU, na.rm = T)
issp_2007$PARTY_LR_CSES_RU_sd <- sd(issp_2007$PARTY_LR_CSES_RU, na.rm = T)
issp_2007$PARTY_LR_CSES_RU_2sd <- issp_2007$PARTY_LR_CSES_RU_sd*2
issp_2007$PARTY_LR_CSES_RU_beta <- (issp_2007$PARTY_LR_CSES_RU-issp_2007$PARTY_LR_CSES_RU_avg)/issp_2007$PARTY_LR_CSES_RU_2sd
table(issp_2007$PARTY_LR_CSES_RU_beta, exclude = NULL)

issp_2007_left_right_model4_EVS_RU <- lmer(PARTY_LR_EVS_RU_beta ~ chronotype_interval_beta + urban_rural_area_beta +
                                             sex_beta + age_beta + degree_beta + income_beta + religiosity_beta + political_interest_beta + (1 + chronotype_interval_beta | country), 
                                           data =  issp_2007)
summary(issp_2007_left_right_model4_EVS_RU)
tab_model(issp_2007_left_right_model4_EVS_RU, show.ci = 0.95)
round(variancePartition::calcVarPart(issp_2007_left_right_model4_EVS_RU)*100,2)
lmerTest::ranova(issp_2007_left_right_model4_EVS_RU)

issp_2007_left_right_model4_CSES_RU <- lmer(PARTY_LR_CSES_RU_beta ~ chronotype_interval_beta + urban_rural_area_beta +
                                              sex_beta + age_beta + degree_beta + income_beta + religiosity_beta + political_interest_beta + (1 + chronotype_interval_beta | country), 
                                            data =  issp_2007)
summary(issp_2007_left_right_model4_CSES_RU)
tab_model(issp_2007_left_right_model4_CSES_RU, show.ci = 0.95)
round(variancePartition::calcVarPart(issp_2007_left_right_model4_CSES_RU)*100,2)
lmerTest::ranova(issp_2007_left_right_model4_CSES_RU)

#### Online Appendices, Table 8A: Multilevel linear modeling of left-right ideological placement with different ideology specifications in Russia in the pooled data ####


stargazer(issp_2007_left_right_model4_EVS_RU, issp_2007_left_right_model4_CSES_RU,
          type = "html", title=" ", digits=2, out="Tables/models_issp_2007_left_right_diff_ideo_spec.htm",
          model.numbers = F,
          column.labels = c("Model 5", "Model 6"),
          covariate.labels = c("Chronotype",
                               "Urban-Rural Area of Residence", "Sex: Male (Base: Female)",
                               "Age", "Education", "Income",
                               "Religious Attendance", "Level on Interest in Politics",
                               "Intercept"))

tab_model(issp_2007_left_right_model4_EVS_RU, show.ci = 0.95)
tab_model(issp_2007_left_right_model4_CSES_RU, show.ci = 0.95)

#### Preparation Online Appendices, Table 8B: Predicted and random effects of chronotype on left-right ideological placement for each country with different ideology specifications in Russia in the pooled data ####

coef(issp_2007_left_right_model4_EVS_RU)
arm::se.coef(issp_2007_left_right_model4_EVS_RU)

ranef(issp_2007_left_right_model4_EVS_RU)
arm::se.ranef(issp_2007_left_right_model4_EVS_RU)

coef(issp_2007_left_right_model4_EVS_RU)$country
coef_issp_2007_left_right_model4_EVS_RU <- data.frame(coef(issp_2007_left_right_model4_EVS_RU)$country)
coef_issp_2007_left_right_model4_EVS_RU <- subset(coef_issp_2007_left_right_model4_EVS_RU, select = c("chronotype_interval_beta"))
coef_issp_2007_left_right_model4_EVS_RU$country <- row.names(coef_issp_2007_left_right_model4_EVS_RU)
names(coef_issp_2007_left_right_model4_EVS_RU)[names(coef_issp_2007_left_right_model4_EVS_RU)=="chronotype_interval_beta"] <- "coef_chronotype_interval_beta"


se_coef_issp_2007_left_right_model4_EVS_RU <- data.frame(arm::se.coef(issp_2007_left_right_model4_EVS_RU)$country)
se_coef_issp_2007_left_right_model4_EVS_RU <- subset(se_coef_issp_2007_left_right_model4_EVS_RU, select = c("chronotype_interval_beta"))
se_coef_issp_2007_left_right_model4_EVS_RU$country <- row.names(se_coef_issp_2007_left_right_model4_EVS_RU)
names(se_coef_issp_2007_left_right_model4_EVS_RU)[names(se_coef_issp_2007_left_right_model4_EVS_RU)=="chronotype_interval_beta"] <- "se_chronotype_interval_beta"

coef_se_issp_2007_left_right_model4_EVS_RU <- merge(coef_issp_2007_left_right_model4_EVS_RU, se_coef_issp_2007_left_right_model4_EVS_RU, by="country")

coef_se_issp_2007_left_right_model4_EVS_RU$chrono.cl.90 <- coef_se_issp_2007_left_right_model4_EVS_RU$coef_chronotype_interval_beta - 1.645*coef_se_issp_2007_left_right_model4_EVS_RU$se_chronotype_interval_beta
coef_se_issp_2007_left_right_model4_EVS_RU$chrono.cu.90 <- coef_se_issp_2007_left_right_model4_EVS_RU$coef_chronotype_interval_beta + 1.645*coef_se_issp_2007_left_right_model4_EVS_RU$se_chronotype_interval_beta
coef_se_issp_2007_left_right_model4_EVS_RU$chrono.cl.95 <- coef_se_issp_2007_left_right_model4_EVS_RU$coef_chronotype_interval_beta - 1.96*coef_se_issp_2007_left_right_model4_EVS_RU$se_chronotype_interval_beta
coef_se_issp_2007_left_right_model4_EVS_RU$chrono.cu.95 <- coef_se_issp_2007_left_right_model4_EVS_RU$coef_chronotype_interval_beta + 1.96*coef_se_issp_2007_left_right_model4_EVS_RU$se_chronotype_interval_beta
coef_se_issp_2007_left_right_model4_EVS_RU$chrono.cl.99 <- coef_se_issp_2007_left_right_model4_EVS_RU$coef_chronotype_interval_beta - 2.576*coef_se_issp_2007_left_right_model4_EVS_RU$se_chronotype_interval_beta
coef_se_issp_2007_left_right_model4_EVS_RU$chrono.cu.99 <- coef_se_issp_2007_left_right_model4_EVS_RU$coef_chronotype_interval_beta + 2.576*coef_se_issp_2007_left_right_model4_EVS_RU$se_chronotype_interval_beta

View(coef_se_issp_2007_left_right_model4_EVS_RU)

ranef(issp_2007_left_right_model4_EVS_RU)$country
ranef_issp_2007_left_right_model4_EVS_RU <- data.frame(ranef(issp_2007_left_right_model4_EVS_RU)$country)
ranef_issp_2007_left_right_model4_EVS_RU <- subset(ranef_issp_2007_left_right_model4_EVS_RU, select = c("chronotype_interval_beta"))
ranef_issp_2007_left_right_model4_EVS_RU$country <- row.names(ranef_issp_2007_left_right_model4_EVS_RU)
names(ranef_issp_2007_left_right_model4_EVS_RU)[names(ranef_issp_2007_left_right_model4_EVS_RU)=="chronotype_interval_beta"] <- "ranef_chronotype_interval_beta"


se_ranef_issp_2007_left_right_model4_EVS_RU <- data.frame(arm::se.ranef(issp_2007_left_right_model4_EVS_RU)$country)
se_ranef_issp_2007_left_right_model4_EVS_RU <- subset(se_ranef_issp_2007_left_right_model4_EVS_RU, select = c("chronotype_interval_beta"))
se_ranef_issp_2007_left_right_model4_EVS_RU$country <- row.names(se_ranef_issp_2007_left_right_model4_EVS_RU)
names(se_ranef_issp_2007_left_right_model4_EVS_RU)[names(se_ranef_issp_2007_left_right_model4_EVS_RU)=="chronotype_interval_beta"] <- "se_chronotype_interval_beta"

ranef_se_issp_2007_left_right_model4_EVS_RU <- merge(ranef_issp_2007_left_right_model4_EVS_RU, se_ranef_issp_2007_left_right_model4_EVS_RU, by="country")

ranef_se_issp_2007_left_right_model4_EVS_RU$chrono.cl.90 <- ranef_se_issp_2007_left_right_model4_EVS_RU$ranef_chronotype_interval_beta - 1.645*ranef_se_issp_2007_left_right_model4_EVS_RU$se_chronotype_interval_beta
ranef_se_issp_2007_left_right_model4_EVS_RU$chrono.cu.90 <- ranef_se_issp_2007_left_right_model4_EVS_RU$ranef_chronotype_interval_beta + 1.645*ranef_se_issp_2007_left_right_model4_EVS_RU$se_chronotype_interval_beta
ranef_se_issp_2007_left_right_model4_EVS_RU$chrono.cl.95 <- ranef_se_issp_2007_left_right_model4_EVS_RU$ranef_chronotype_interval_beta - 1.96*ranef_se_issp_2007_left_right_model4_EVS_RU$se_chronotype_interval_beta
ranef_se_issp_2007_left_right_model4_EVS_RU$chrono.cu.95 <- ranef_se_issp_2007_left_right_model4_EVS_RU$ranef_chronotype_interval_beta + 1.96*ranef_se_issp_2007_left_right_model4_EVS_RU$se_chronotype_interval_beta
ranef_se_issp_2007_left_right_model4_EVS_RU$chrono.cl.99 <- ranef_se_issp_2007_left_right_model4_EVS_RU$ranef_chronotype_interval_beta - 2.576*ranef_se_issp_2007_left_right_model4_EVS_RU$se_chronotype_interval_beta
ranef_se_issp_2007_left_right_model4_EVS_RU$chrono.cu.99 <- ranef_se_issp_2007_left_right_model4_EVS_RU$ranef_chronotype_interval_beta + 2.576*ranef_se_issp_2007_left_right_model4_EVS_RU$se_chronotype_interval_beta

View(ranef_se_issp_2007_left_right_model4_EVS_RU)

coef(issp_2007_left_right_model4_CSES_RU)
arm::se.coef(issp_2007_left_right_model4_CSES_RU)

ranef(issp_2007_left_right_model4_CSES_RU)
arm::se.ranef(issp_2007_left_right_model4_CSES_RU)

coef(issp_2007_left_right_model4_CSES_RU)$country
coef_issp_2007_left_right_model4_CSES_RU <- data.frame(coef(issp_2007_left_right_model4_CSES_RU)$country)
coef_issp_2007_left_right_model4_CSES_RU <- subset(coef_issp_2007_left_right_model4_CSES_RU, select = c("chronotype_interval_beta"))
coef_issp_2007_left_right_model4_CSES_RU$country <- row.names(coef_issp_2007_left_right_model4_CSES_RU)
names(coef_issp_2007_left_right_model4_CSES_RU)[names(coef_issp_2007_left_right_model4_CSES_RU)=="chronotype_interval_beta"] <- "coef_chronotype_interval_beta"


se_coef_issp_2007_left_right_model4_CSES_RU <- data.frame(arm::se.coef(issp_2007_left_right_model4_CSES_RU)$country)
se_coef_issp_2007_left_right_model4_CSES_RU <- subset(se_coef_issp_2007_left_right_model4_CSES_RU, select = c("chronotype_interval_beta"))
se_coef_issp_2007_left_right_model4_CSES_RU$country <- row.names(se_coef_issp_2007_left_right_model4_CSES_RU)
names(se_coef_issp_2007_left_right_model4_CSES_RU)[names(se_coef_issp_2007_left_right_model4_CSES_RU)=="chronotype_interval_beta"] <- "se_chronotype_interval_beta"

coef_se_issp_2007_left_right_model4_CSES_RU <- merge(coef_issp_2007_left_right_model4_CSES_RU, se_coef_issp_2007_left_right_model4_CSES_RU, by="country")

coef_se_issp_2007_left_right_model4_CSES_RU$chrono.cl.90 <- coef_se_issp_2007_left_right_model4_CSES_RU$coef_chronotype_interval_beta - 1.645*coef_se_issp_2007_left_right_model4_CSES_RU$se_chronotype_interval_beta
coef_se_issp_2007_left_right_model4_CSES_RU$chrono.cu.90 <- coef_se_issp_2007_left_right_model4_CSES_RU$coef_chronotype_interval_beta + 1.645*coef_se_issp_2007_left_right_model4_CSES_RU$se_chronotype_interval_beta
coef_se_issp_2007_left_right_model4_CSES_RU$chrono.cl.95 <- coef_se_issp_2007_left_right_model4_CSES_RU$coef_chronotype_interval_beta - 1.96*coef_se_issp_2007_left_right_model4_CSES_RU$se_chronotype_interval_beta
coef_se_issp_2007_left_right_model4_CSES_RU$chrono.cu.95 <- coef_se_issp_2007_left_right_model4_CSES_RU$coef_chronotype_interval_beta + 1.96*coef_se_issp_2007_left_right_model4_CSES_RU$se_chronotype_interval_beta
coef_se_issp_2007_left_right_model4_CSES_RU$chrono.cl.99 <- coef_se_issp_2007_left_right_model4_CSES_RU$coef_chronotype_interval_beta - 2.576*coef_se_issp_2007_left_right_model4_CSES_RU$se_chronotype_interval_beta
coef_se_issp_2007_left_right_model4_CSES_RU$chrono.cu.99 <- coef_se_issp_2007_left_right_model4_CSES_RU$coef_chronotype_interval_beta + 2.576*coef_se_issp_2007_left_right_model4_CSES_RU$se_chronotype_interval_beta

View(coef_se_issp_2007_left_right_model4_CSES_RU)

ranef(issp_2007_left_right_model4_CSES_RU)$country
ranef_issp_2007_left_right_model4_CSES_RU <- data.frame(ranef(issp_2007_left_right_model4_CSES_RU)$country)
ranef_issp_2007_left_right_model4_CSES_RU <- subset(ranef_issp_2007_left_right_model4_CSES_RU, select = c("chronotype_interval_beta"))
ranef_issp_2007_left_right_model4_CSES_RU$country <- row.names(ranef_issp_2007_left_right_model4_CSES_RU)
names(ranef_issp_2007_left_right_model4_CSES_RU)[names(ranef_issp_2007_left_right_model4_CSES_RU)=="chronotype_interval_beta"] <- "ranef_chronotype_interval_beta"


se_ranef_issp_2007_left_right_model4_CSES_RU <- data.frame(arm::se.ranef(issp_2007_left_right_model4_CSES_RU)$country)
se_ranef_issp_2007_left_right_model4_CSES_RU <- subset(se_ranef_issp_2007_left_right_model4_CSES_RU, select = c("chronotype_interval_beta"))
se_ranef_issp_2007_left_right_model4_CSES_RU$country <- row.names(se_ranef_issp_2007_left_right_model4_CSES_RU)
names(se_ranef_issp_2007_left_right_model4_CSES_RU)[names(se_ranef_issp_2007_left_right_model4_CSES_RU)=="chronotype_interval_beta"] <- "se_chronotype_interval_beta"

ranef_se_issp_2007_left_right_model4_CSES_RU <- merge(ranef_issp_2007_left_right_model4_CSES_RU, se_ranef_issp_2007_left_right_model4_CSES_RU, by="country")

ranef_se_issp_2007_left_right_model4_CSES_RU$chrono.cl.90 <- ranef_se_issp_2007_left_right_model4_CSES_RU$ranef_chronotype_interval_beta - 1.645*ranef_se_issp_2007_left_right_model4_CSES_RU$se_chronotype_interval_beta
ranef_se_issp_2007_left_right_model4_CSES_RU$chrono.cu.90 <- ranef_se_issp_2007_left_right_model4_CSES_RU$ranef_chronotype_interval_beta + 1.645*ranef_se_issp_2007_left_right_model4_CSES_RU$se_chronotype_interval_beta
ranef_se_issp_2007_left_right_model4_CSES_RU$chrono.cl.95 <- ranef_se_issp_2007_left_right_model4_CSES_RU$ranef_chronotype_interval_beta - 1.96*ranef_se_issp_2007_left_right_model4_CSES_RU$se_chronotype_interval_beta
ranef_se_issp_2007_left_right_model4_CSES_RU$chrono.cu.95 <- ranef_se_issp_2007_left_right_model4_CSES_RU$ranef_chronotype_interval_beta + 1.96*ranef_se_issp_2007_left_right_model4_CSES_RU$se_chronotype_interval_beta
ranef_se_issp_2007_left_right_model4_CSES_RU$chrono.cl.99 <- ranef_se_issp_2007_left_right_model4_CSES_RU$ranef_chronotype_interval_beta - 2.576*ranef_se_issp_2007_left_right_model4_CSES_RU$se_chronotype_interval_beta
ranef_se_issp_2007_left_right_model4_CSES_RU$chrono.cu.99 <- ranef_se_issp_2007_left_right_model4_CSES_RU$ranef_chronotype_interval_beta + 2.576*ranef_se_issp_2007_left_right_model4_CSES_RU$se_chronotype_interval_beta

View(ranef_se_issp_2007_left_right_model4_CSES_RU)

#### Multinomial modeling of affiliated parties ####

#### Ireland ####

Ireland <- subset(issp_2007, select = c("PARTY_LR", "urban_rural_area", "sex",  "age",  "degree",  "income",  "political_interest",  "religiosity", "voted_parties"), subset=country=="Ireland") 

table(Ireland$voted_parties, exclude = NULL)

Ireland$affiliated_party <- NA
Ireland$affiliated_party[Ireland$voted_parties=="IE - Fianna Fail"] <- "Fianna Fáil"
Ireland$affiliated_party[Ireland$voted_parties=="IE - Fine Gael"] <- "Fine Gael"
Ireland$affiliated_party[Ireland$voted_parties=="IE - Missing"] <- "Missing Responses"
Ireland$affiliated_party[Ireland$voted_parties=="IE - Labour"] <- "Labour Party"
Ireland$affiliated_party[Ireland$voted_parties=="IE - Sinn Fein"] <- "Sinn Féin"
Ireland$affiliated_party[Ireland$voted_parties=="IE - Green Party" |
                           Ireland$voted_parties=="IE - Other party" | 
                           Ireland$voted_parties=="IE - Progressive Democrats"] <- "Other Parties"
table(Ireland$affiliated_party, exclude = NULL)

Ireland$affiliated_party <- factor(Ireland$affiliated_party, levels = c("Missing Responses",
                                                                        "Fianna Fáil", "Fine Gael", "Labour Party",
                                                                        "Other Parties", "Sinn Féin"))
Ireland$PARTY_LR_avg <- mean(Ireland$PARTY_LR, na.rm = T)
Ireland$PARTY_LR_sd <- sd(Ireland$PARTY_LR, na.rm = T)
Ireland$PARTY_LR_2sd <- Ireland$PARTY_LR_sd*2
Ireland$PARTY_LR_beta <- (Ireland$PARTY_LR-Ireland$PARTY_LR_avg)/Ireland$PARTY_LR_2sd
table(Ireland$PARTY_LR_beta, exclude = NULL)

Ireland$religiosity_avg <- mean(Ireland$religiosity, na.rm = T)
Ireland$religiosity_sd <- sd(Ireland$religiosity, na.rm = T)
Ireland$religiosity_2sd <- Ireland$religiosity_sd*2
Ireland$religiosity_beta <- (Ireland$religiosity-Ireland$religiosity_avg)/Ireland$religiosity_2sd
table(Ireland$religiosity_beta, exclude = NULL)

table(Ireland$urban_rural_area, exclude = NULL)
Ireland$urban_rural_area_avg <- mean(Ireland$urban_rural_area, na.rm = T)
Ireland$urban_rural_area_sd <- sd(Ireland$urban_rural_area, na.rm = T)
Ireland$urban_rural_area_2sd <- Ireland$urban_rural_area_sd*2
Ireland$urban_rural_area_beta <- (Ireland$urban_rural_area-Ireland$urban_rural_area_avg)/Ireland$urban_rural_area_2sd

table(Ireland$age, exclude = NULL)
Ireland$age_avg <- mean(Ireland$age, na.rm = T)
Ireland$age_sd <- sd(Ireland$age, na.rm = T)
Ireland$age_2sd <- Ireland$age_sd*2
Ireland$age_beta <- (Ireland$age-Ireland$age_avg)/Ireland$age_2sd

table(Ireland$degree, exclude = NULL)
Ireland$degree_avg <- mean(Ireland$degree, na.rm = T)
Ireland$degree_sd <- sd(Ireland$degree, na.rm = T)
Ireland$degree_2sd <- Ireland$degree_sd*2
Ireland$degree_beta <- (Ireland$degree-Ireland$degree_avg)/Ireland$degree_2sd

Ireland$sex_dummy_avg <- mean(Ireland$sex, na.rm = T)
Ireland$sex_beta <- (Ireland$sex-Ireland$sex_dummy_avg)

Ireland$political_interest_avg <- mean(Ireland$political_interest, na.rm = T)
Ireland$political_interest_sd <- sd(Ireland$political_interest, na.rm = T)
Ireland$political_interest_2sd <- Ireland$political_interest_sd*2
Ireland$political_interest_beta <- (Ireland$political_interest-Ireland$political_interest_avg)/Ireland$political_interest_2sd

Ireland$income_avg <- mean(Ireland$income, na.rm = T)
Ireland$income_sd <- sd(Ireland$income, na.rm = T)
Ireland$income_2sd <- Ireland$income_sd*2
Ireland$income_beta <- (Ireland$income-Ireland$income_avg)/Ireland$income_2sd

library(nnet)

multinom_IE_m1 <- multinom(affiliated_party ~ PARTY_LR_beta + age_beta + degree_beta + income_beta + political_interest_beta + sex_beta + urban_rural_area_beta, data = Ireland)
summary(multinom_IE_m1)
tab_model(multinom_IE_m1, show.ci = .95)

multinom_IE_m2 <- multinom(affiliated_party ~ religiosity_beta + age_beta + degree_beta + income_beta + political_interest_beta + sex_beta + urban_rural_area_beta, data = Ireland)
summary(multinom_IE_m2)
tab_model(multinom_IE_m2, show.ci = .95)

#### Online Appendices, Table 9: Multinomial modeling of party affiliations in Ireland ####


stargazer(multinom_IE_m1, multinom_IE_m2,
          type = "html", title=" ", digits=2, out="Tables/affiliation_models_ie.htm",
          model.numbers = F,
          covariate.labels = c("Left-Right Ideological Placement",
                               "Religious Attendance",
                               "Age",
                               "Education",
                               "Income",
                               "Level on Interest in Politics",
                               "Sex: Male (Base: Female)",
                               "Urban-Rural Area of Residence", 
                               "Intercept"))

library(AER)
coeftest(multinom_IE_m1)
coeftest(multinom_IE_m2)

#### Mexico ####

Mexico <- subset(issp_2007, select = c("PARTY_LR", "urban_rural_area", "sex",  "age",  "degree",  "income",  "political_interest",  "religiosity", "voted_parties"), subset=country=="Mexico") 

table(Mexico$voted_parties, exclude = NULL)

Mexico$affiliated_party <- NA
Mexico$affiliated_party[Mexico$voted_parties=="MX - pan"] <- "Partido Acción Nacional"
Mexico$affiliated_party[Mexico$voted_parties=="MX - prd"] <- "Partido de la Revolución Democrática"
Mexico$affiliated_party[Mexico$voted_parties=="MX - Missing"] <- "Missing Responses"
Mexico$affiliated_party[Mexico$voted_parties=="MX - pri"] <- "Partido Revolucionario Institucional"
Mexico$affiliated_party[Mexico$voted_parties=="MX - Convergencia" |
                          Mexico$voted_parties=="MX - Other party" | 
                          Mexico$voted_parties=="MX - panal" |
                          Mexico$voted_parties=="MX - pt" |
                          Mexico$voted_parties=="MX - pvem"] <- "Other Parties"
table(Mexico$affiliated_party, exclude = NULL)

Mexico$affiliated_party <- factor(Mexico$affiliated_party, levels = c("Missing Responses",
                                                                      "Partido Acción Nacional",
                                                                      "Partido de la Revolución Democrática",
                                                                      "Partido Revolucionario Institucional",
                                                                      "Other Parties"))
table(Mexico$affiliated_party, Mexico$PARTY_LR, exclude = NULL)


Mexico$affiliated_party_alt <- Mexico$affiliated_party
Mexico$affiliated_party_alt <- ifelse(Mexico$affiliated_party_alt=="Missing Responses", NA, Mexico$affiliated_party_alt)
Mexico$affiliated_party_alt <- factor(Mexico$affiliated_party, levels = c( "Partido Revolucionario Institucional",
                                                                           "Partido Acción Nacional",
                                                                           "Partido de la Revolución Democrática",
                                                                           "Other Parties"))
table(Mexico$affiliated_party_alt, exclude = NULL)

Mexico$PARTY_LR_avg <- mean(Mexico$PARTY_LR, na.rm = T)
Mexico$PARTY_LR_sd <- sd(Mexico$PARTY_LR, na.rm = T)
Mexico$PARTY_LR_2sd <- Mexico$PARTY_LR_sd*2
Mexico$PARTY_LR_beta <- (Mexico$PARTY_LR-Mexico$PARTY_LR_avg)/Mexico$PARTY_LR_2sd
table(Mexico$PARTY_LR_beta, exclude = NULL)

Mexico$religiosity_avg <- mean(Mexico$religiosity, na.rm = T)
Mexico$religiosity_sd <- sd(Mexico$religiosity, na.rm = T)
Mexico$religiosity_2sd <- Mexico$religiosity_sd*2
Mexico$religiosity_beta <- (Mexico$religiosity-Mexico$religiosity_avg)/Mexico$religiosity_2sd
table(Mexico$religiosity_beta, exclude = NULL)

table(Mexico$urban_rural_area, exclude = NULL)
Mexico$urban_rural_area_avg <- mean(Mexico$urban_rural_area, na.rm = T)
Mexico$urban_rural_area_sd <- sd(Mexico$urban_rural_area, na.rm = T)
Mexico$urban_rural_area_2sd <- Mexico$urban_rural_area_sd*2
Mexico$urban_rural_area_beta <- (Mexico$urban_rural_area-Mexico$urban_rural_area_avg)/Mexico$urban_rural_area_2sd

table(Mexico$age, exclude = NULL)
Mexico$age_avg <- mean(Mexico$age, na.rm = T)
Mexico$age_sd <- sd(Mexico$age, na.rm = T)
Mexico$age_2sd <- Mexico$age_sd*2
Mexico$age_beta <- (Mexico$age-Mexico$age_avg)/Mexico$age_2sd

table(Mexico$degree, exclude = NULL)
Mexico$degree_avg <- mean(Mexico$degree, na.rm = T)
Mexico$degree_sd <- sd(Mexico$degree, na.rm = T)
Mexico$degree_2sd <- Mexico$degree_sd*2
Mexico$degree_beta <- (Mexico$degree-Mexico$degree_avg)/Mexico$degree_2sd

Mexico$sex_dummy_avg <- mean(Mexico$sex, na.rm = T)
Mexico$sex_beta <- (Mexico$sex-Mexico$sex_dummy_avg)

Mexico$political_interest_avg <- mean(Mexico$political_interest, na.rm = T)
Mexico$political_interest_sd <- sd(Mexico$political_interest, na.rm = T)
Mexico$political_interest_2sd <- Mexico$political_interest_sd*2
Mexico$political_interest_beta <- (Mexico$political_interest-Mexico$political_interest_avg)/Mexico$political_interest_2sd

Mexico$income_avg <- mean(Mexico$income, na.rm = T)
Mexico$income_sd <- sd(Mexico$income, na.rm = T)
Mexico$income_2sd <- Mexico$income_sd*2
Mexico$income_beta <- (Mexico$income-Mexico$income_avg)/Mexico$income_2sd

library(nnet)

multinom_MX_m1 <- multinom(affiliated_party_alt ~ PARTY_LR_beta + age_beta + degree_beta + income_beta + political_interest_beta + sex_beta + urban_rural_area_beta, data = Mexico)
summary(multinom_MX_m1)
tab_model(multinom_MX_m1, show.ci = .95)

multinom_MX_m2 <- multinom(affiliated_party ~ religiosity_beta + age_beta + degree_beta + income_beta + political_interest_beta + sex_beta + urban_rural_area_beta, data = Mexico)
summary(multinom_MX_m2)
tab_model(multinom_MX_m2, show.ci = .95)

#### Online Appendices, Table 10: Multinomial modeling of party affiliations in Mexico ####


stargazer(multinom_MX_m1, multinom_MX_m2,
          type = "html", title=" ", digits=2, out="Tables/affiliation_models_mx.htm",
          model.numbers = F,
          covariate.labels = c("Left-Right Ideological Placement",
                               "Religious Attendance",
                               "Age",
                               "Education",
                               "Income",
                               "Level on Interest in Politics",
                               "Sex: Male (Base: Female)",
                               "Urban-Rural Area of Residence", 
                               "Intercept"))

library(AER)
coeftest(multinom_MX_m1)
coeftest(multinom_MX_m2)

#### New Zealand ####

New_Zealand <- subset(issp_2007, select = c("PARTY_LR", "urban_rural_area", "sex",  "age",  "degree",  "income",  "political_interest",  "religiosity", "voted_parties"), subset=country=="New Zealand") 

table(New_Zealand$voted_parties, exclude = NULL)

New_Zealand$affiliated_party <- NA
New_Zealand$affiliated_party[New_Zealand$voted_parties=="NZ - Labour"] <- "Labour Party"
New_Zealand$affiliated_party[New_Zealand$voted_parties=="NZ - National"] <- "National Party"
New_Zealand$affiliated_party[New_Zealand$voted_parties=="NZ - Missing"] <- "Missing Responses"
New_Zealand$affiliated_party <- ifelse(is.na(New_Zealand$affiliated_party), "Other Parties", New_Zealand$affiliated_party)
table(New_Zealand$affiliated_party, exclude = NULL)
New_Zealand$affiliated_party <- factor(New_Zealand$affiliated_party, levels = c("National Party", "Labour Party", "Other Parties", "Missing Responses"), ordered = F)

New_Zealand$PARTY_LR_avg <- mean(New_Zealand$PARTY_LR, na.rm = T)
New_Zealand$PARTY_LR_sd <- sd(New_Zealand$PARTY_LR, na.rm = T)
New_Zealand$PARTY_LR_2sd <- New_Zealand$PARTY_LR_sd*2
New_Zealand$PARTY_LR_beta <- (New_Zealand$PARTY_LR-New_Zealand$PARTY_LR_avg)/New_Zealand$PARTY_LR_2sd
table(New_Zealand$PARTY_LR_beta, exclude = NULL)

New_Zealand$religiosity_avg <- mean(New_Zealand$religiosity, na.rm = T)
New_Zealand$religiosity_sd <- sd(New_Zealand$religiosity, na.rm = T)
New_Zealand$religiosity_2sd <- New_Zealand$religiosity_sd*2
New_Zealand$religiosity_beta <- (New_Zealand$religiosity-New_Zealand$religiosity_avg)/New_Zealand$religiosity_2sd
table(New_Zealand$religiosity_beta, exclude = NULL)

table(New_Zealand$urban_rural_area, exclude = NULL)
New_Zealand$urban_rural_area_avg <- mean(New_Zealand$urban_rural_area, na.rm = T)
New_Zealand$urban_rural_area_sd <- sd(New_Zealand$urban_rural_area, na.rm = T)
New_Zealand$urban_rural_area_2sd <- New_Zealand$urban_rural_area_sd*2
New_Zealand$urban_rural_area_beta <- (New_Zealand$urban_rural_area-New_Zealand$urban_rural_area_avg)/New_Zealand$urban_rural_area_2sd

table(New_Zealand$age, exclude = NULL)
New_Zealand$age_avg <- mean(New_Zealand$age, na.rm = T)
New_Zealand$age_sd <- sd(New_Zealand$age, na.rm = T)
New_Zealand$age_2sd <- New_Zealand$age_sd*2
New_Zealand$age_beta <- (New_Zealand$age-New_Zealand$age_avg)/New_Zealand$age_2sd

table(New_Zealand$degree, exclude = NULL)
New_Zealand$degree_avg <- mean(New_Zealand$degree, na.rm = T)
New_Zealand$degree_sd <- sd(New_Zealand$degree, na.rm = T)
New_Zealand$degree_2sd <- New_Zealand$degree_sd*2
New_Zealand$degree_beta <- (New_Zealand$degree-New_Zealand$degree_avg)/New_Zealand$degree_2sd

New_Zealand$sex_dummy_avg <- mean(New_Zealand$sex, na.rm = T)
New_Zealand$sex_beta <- (New_Zealand$sex-New_Zealand$sex_dummy_avg)

New_Zealand$political_interest_avg <- mean(New_Zealand$political_interest, na.rm = T)
New_Zealand$political_interest_sd <- sd(New_Zealand$political_interest, na.rm = T)
New_Zealand$political_interest_2sd <- New_Zealand$political_interest_sd*2
New_Zealand$political_interest_beta <- (New_Zealand$political_interest-New_Zealand$political_interest_avg)/New_Zealand$political_interest_2sd

New_Zealand$income_avg <- mean(New_Zealand$income, na.rm = T)
New_Zealand$income_sd <- sd(New_Zealand$income, na.rm = T)
New_Zealand$income_2sd <- New_Zealand$income_sd*2
New_Zealand$income_beta <- (New_Zealand$income-New_Zealand$income_avg)/New_Zealand$income_2sd

library(nnet)

multinom_NZ_m1 <- multinom(affiliated_party ~ PARTY_LR_beta + age_beta + degree_beta + income_beta + political_interest_beta + sex_beta + urban_rural_area_beta, data = New_Zealand)
summary(multinom_NZ_m1)
tab_model(multinom_NZ_m1, show.ci = .95)

multinom_NZ_m2 <- multinom(affiliated_party ~ religiosity_beta + age_beta + degree_beta + income_beta + political_interest_beta + sex_beta + urban_rural_area_beta, data = New_Zealand)
summary(multinom_NZ_m2)
tab_model(multinom_NZ_m2, show.ci = .95)

#### Online Appendices, Table 11: Multinomial modeling of party affiliations in New Zealand ####


stargazer(multinom_NZ_m1, multinom_NZ_m2,
          type = "html", title=" ", digits=2, out="Tables/affiliation_models_nz.htm",
          model.numbers = F,
          covariate.labels = c("Left-Right Ideological Placement",
                               "Religious Attendance",
                               "Age",
                               "Education",
                               "Income",
                               "Level on Interest in Politics",
                               "Sex: Male (Base: Female)",
                               "Urban-Rural Area of Residence", 
                               "Intercept"))

library(AER)
coeftest(multinom_NZ_m1)
coeftest(multinom_NZ_m2)

#### the Philippines ####

Philippines <- subset(issp_2007, select = c("PARTY_LR", "urban_rural_area", "sex",  "age",  "degree",  "income",  "political_interest",  "religiosity", "voted_parties"), subset=country=="the Philippines") 

table(Philippines$voted_parties, exclude = NULL)

Philippines$affiliated_party <- NA
Philippines$affiliated_party[Philippines$voted_parties=="PH - laban" |
                               Philippines$voted_parties=="PH - LABAN SA PARTIDO NI GLORIA" |
                               Philippines$voted_parties=="PH - MASANG PILIPINO" |
                               Philippines$voted_parties=="PH - nationalista" |
                               Philippines$voted_parties=="PH - NPC-NATIONAL PEOPLE COALITION" |
                               Philippines$voted_parties=="PH - PARTIDO NI ERAP" |
                               Philippines$voted_parties=="PH - LIBERAL PARTY" |
                               Philippines$voted_parties=="PH - OPOSISYON/OPPOSITION"] <- "Genuine Opposition"
Philippines$affiliated_party[Philippines$voted_parties=="PH - CHRISTIAN MUSLIM DEMOCRATIC FEDERATION" |
                               Philippines$voted_parties== "PH - kampi" |
                               Philippines$voted_parties=="PH - OMPIA PARTY" |
                               Philippines$voted_parties=="PH - lakas" |
                               Philippines$voted_parties=="PH - LAKAS-NUCD-CMD" |
                               Philippines$voted_parties=="PH - administration"] <- "LAKAS-NUCD-CMD"
Philippines$affiliated_party[Philippines$voted_parties=="PH - Missing"] <- "Missing Responses"
Philippines$affiliated_party <- ifelse(is.na(Philippines$affiliated_party), "Other Parties", Philippines$affiliated_party)
table(Philippines$affiliated_party, exclude = NULL)
Philippines$affiliated_party <- factor(Philippines$affiliated_party, levels = c("Missing Responses", "Genuine Opposition",
                                                                                "LAKAS-NUCD-CMD", "Other Parties"), ordered = F)

Philippines$PARTY_LR_avg <- mean(Philippines$PARTY_LR, na.rm = T)
Philippines$PARTY_LR_sd <- sd(Philippines$PARTY_LR, na.rm = T)
Philippines$PARTY_LR_2sd <- Philippines$PARTY_LR_sd*2
Philippines$PARTY_LR_beta <- (Philippines$PARTY_LR-Philippines$PARTY_LR_avg)/Philippines$PARTY_LR_2sd
table(Philippines$PARTY_LR_beta, exclude = NULL)

Philippines$religiosity_avg <- mean(Philippines$religiosity, na.rm = T)
Philippines$religiosity_sd <- sd(Philippines$religiosity, na.rm = T)
Philippines$religiosity_2sd <- Philippines$religiosity_sd*2
Philippines$religiosity_beta <- (Philippines$religiosity-Philippines$religiosity_avg)/Philippines$religiosity_2sd
table(Philippines$religiosity_beta, exclude = NULL)

table(Philippines$urban_rural_area, exclude = NULL)
Philippines$urban_rural_area_avg <- mean(Philippines$urban_rural_area, na.rm = T)
Philippines$urban_rural_area_sd <- sd(Philippines$urban_rural_area, na.rm = T)
Philippines$urban_rural_area_2sd <- Philippines$urban_rural_area_sd*2
Philippines$urban_rural_area_beta <- (Philippines$urban_rural_area-Philippines$urban_rural_area_avg)/Philippines$urban_rural_area_2sd

table(Philippines$age, exclude = NULL)
Philippines$age_avg <- mean(Philippines$age, na.rm = T)
Philippines$age_sd <- sd(Philippines$age, na.rm = T)
Philippines$age_2sd <- Philippines$age_sd*2
Philippines$age_beta <- (Philippines$age-Philippines$age_avg)/Philippines$age_2sd

table(Philippines$degree, exclude = NULL)
Philippines$degree_avg <- mean(Philippines$degree, na.rm = T)
Philippines$degree_sd <- sd(Philippines$degree, na.rm = T)
Philippines$degree_2sd <- Philippines$degree_sd*2
Philippines$degree_beta <- (Philippines$degree-Philippines$degree_avg)/Philippines$degree_2sd

Philippines$sex_dummy_avg <- mean(Philippines$sex, na.rm = T)
Philippines$sex_beta <- (Philippines$sex-Philippines$sex_dummy_avg)

Philippines$political_interest_avg <- mean(Philippines$political_interest, na.rm = T)
Philippines$political_interest_sd <- sd(Philippines$political_interest, na.rm = T)
Philippines$political_interest_2sd <- Philippines$political_interest_sd*2
Philippines$political_interest_beta <- (Philippines$political_interest-Philippines$political_interest_avg)/Philippines$political_interest_2sd

Philippines$income_avg <- mean(Philippines$income, na.rm = T)
Philippines$income_sd <- sd(Philippines$income, na.rm = T)
Philippines$income_2sd <- Philippines$income_sd*2
Philippines$income_beta <- (Philippines$income-Philippines$income_avg)/Philippines$income_2sd

library(nnet)

multinom_PH_m1 <- multinom(affiliated_party ~ PARTY_LR_beta + age_beta + degree_beta + income_beta + political_interest_beta + sex_beta + urban_rural_area_beta, data = Philippines)
summary(multinom_PH_m1)
tab_model(multinom_PH_m1, show.ci = .95)

multinom_PH_m2 <- multinom(affiliated_party ~ religiosity_beta + age_beta + degree_beta + income_beta + political_interest_beta + sex_beta + urban_rural_area_beta, data = Philippines)
summary(multinom_PH_m2)
tab_model(multinom_PH_m2, show.ci = .95)

#### Online Appendices, Table 12: Multinomial modeling of party affiliations in the Philippines ####

stargazer(multinom_PH_m1, multinom_PH_m2,
          type = "html", title=" ", digits=2, out="Tables/affiliation_models_ph.htm",
          model.numbers = F,
          covariate.labels = c("Left-Right Ideological Placement",
                               "Religious Attendance",
                               "Age",
                               "Education",
                               "Income",
                               "Level on Interest in Politics",
                               "Sex: Male (Base: Female)",
                               "Urban-Rural Area of Residence", 
                               "Intercept"))

library(AER)
coeftest(multinom_PH_m1)
coeftest(multinom_PH_m2)

#### South Korea ####

South_Korea <- subset(issp_2007, select = c("PARTY_LR", "urban_rural_area", "sex",  "age",  "degree",  "income",  "political_interest",  "religiosity", "voted_parties"), subset=country=="South Korea") 

table(South_Korea$voted_parties, exclude = NULL)

South_Korea$affiliated_party <- NA
South_Korea$affiliated_party[South_Korea$voted_parties=="KR - Democratic Labor Party"] <- "Democratic Labor Party"
South_Korea$affiliated_party[South_Korea$voted_parties=="KR - Grand National Party"] <- "Grand National Party"
South_Korea$affiliated_party[South_Korea$voted_parties=="KR - Uri Party"] <- "Uri Party"
South_Korea$affiliated_party[South_Korea$voted_parties=="KR - Missing"] <- "Missing Responses"
South_Korea$affiliated_party <- ifelse(is.na(South_Korea$affiliated_party), "Other Parties", South_Korea$affiliated_party)
table(South_Korea$affiliated_party, exclude = NULL)
South_Korea$affiliated_party <- factor(South_Korea$affiliated_party, levels = c("Grand National Party",
                                                                                "Democratic Labor Party",
                                                                                "Uri Party",
                                                                                "Other Parties",
                                                                                "Missing Responses"), ordered = F)

South_Korea$PARTY_LR_avg <- mean(South_Korea$PARTY_LR, na.rm = T)
South_Korea$PARTY_LR_sd <- sd(South_Korea$PARTY_LR, na.rm = T)
South_Korea$PARTY_LR_2sd <- South_Korea$PARTY_LR_sd*2
South_Korea$PARTY_LR_beta <- (South_Korea$PARTY_LR-South_Korea$PARTY_LR_avg)/South_Korea$PARTY_LR_2sd
table(South_Korea$PARTY_LR_beta, exclude = NULL)

South_Korea$religiosity_avg <- mean(South_Korea$religiosity, na.rm = T)
South_Korea$religiosity_sd <- sd(South_Korea$religiosity, na.rm = T)
South_Korea$religiosity_2sd <- South_Korea$religiosity_sd*2
South_Korea$religiosity_beta <- (South_Korea$religiosity-South_Korea$religiosity_avg)/South_Korea$religiosity_2sd
table(South_Korea$religiosity_beta, exclude = NULL)

table(South_Korea$urban_rural_area, exclude = NULL)
South_Korea$urban_rural_area_avg <- mean(South_Korea$urban_rural_area, na.rm = T)
South_Korea$urban_rural_area_sd <- sd(South_Korea$urban_rural_area, na.rm = T)
South_Korea$urban_rural_area_2sd <- South_Korea$urban_rural_area_sd*2
South_Korea$urban_rural_area_beta <- (South_Korea$urban_rural_area-South_Korea$urban_rural_area_avg)/South_Korea$urban_rural_area_2sd

table(South_Korea$age, exclude = NULL)
South_Korea$age_avg <- mean(South_Korea$age, na.rm = T)
South_Korea$age_sd <- sd(South_Korea$age, na.rm = T)
South_Korea$age_2sd <- South_Korea$age_sd*2
South_Korea$age_beta <- (South_Korea$age-South_Korea$age_avg)/South_Korea$age_2sd

table(South_Korea$degree, exclude = NULL)
South_Korea$degree_avg <- mean(South_Korea$degree, na.rm = T)
South_Korea$degree_sd <- sd(South_Korea$degree, na.rm = T)
South_Korea$degree_2sd <- South_Korea$degree_sd*2
South_Korea$degree_beta <- (South_Korea$degree-South_Korea$degree_avg)/South_Korea$degree_2sd

South_Korea$sex_dummy_avg <- mean(South_Korea$sex, na.rm = T)
South_Korea$sex_beta <- (South_Korea$sex-South_Korea$sex_dummy_avg)

South_Korea$political_interest_avg <- mean(South_Korea$political_interest, na.rm = T)
South_Korea$political_interest_sd <- sd(South_Korea$political_interest, na.rm = T)
South_Korea$political_interest_2sd <- South_Korea$political_interest_sd*2
South_Korea$political_interest_beta <- (South_Korea$political_interest-South_Korea$political_interest_avg)/South_Korea$political_interest_2sd

South_Korea$income_avg <- mean(South_Korea$income, na.rm = T)
South_Korea$income_sd <- sd(South_Korea$income, na.rm = T)
South_Korea$income_2sd <- South_Korea$income_sd*2
South_Korea$income_beta <- (South_Korea$income-South_Korea$income_avg)/South_Korea$income_2sd

library(nnet)

multinom_KR_m1 <- multinom(affiliated_party ~ PARTY_LR_beta + age_beta + degree_beta + income_beta + political_interest_beta + sex_beta + urban_rural_area_beta, data = South_Korea)
summary(multinom_KR_m1)
tab_model(multinom_KR_m1, show.ci = .95)

multinom_KR_m2 <- multinom(affiliated_party ~ religiosity_beta + age_beta + degree_beta + income_beta + political_interest_beta + sex_beta + urban_rural_area_beta, data = South_Korea)
summary(multinom_KR_m2)
tab_model(multinom_KR_m2, show.ci = .95)

#### Online Appendices, Table 13: Multinomial modeling of party affiliations in South Korea ####

stargazer(multinom_KR_m1, multinom_KR_m2,
          type = "html", title=" ", digits=2, out="Tables/affiliation_models_kr.htm",
          model.numbers = F,
          covariate.labels = c("Left-Right Ideological Placement",
                               "Religious Attendance",
                               "Age",
                               "Education",
                               "Income",
                               "Level on Interest in Politics",
                               "Sex: Male (Base: Female)",
                               "Urban-Rural Area of Residence", 
                               "Intercept"))

library(AER)
coeftest(multinom_KR_m1)
coeftest(multinom_KR_m2)

#### Russia ####

Russia <- subset(issp_2007, select = c("PARTY_LR", "urban_rural_area", "sex",  "age",  "degree",  "income",  "political_interest",  "religiosity", "voted_parties"), subset=country=="Russia") 

table(Russia$voted_parties, exclude = NULL)

Russia$affiliated_party <- NA
Russia$affiliated_party[Russia$voted_parties=="RU - Against all/ threw out/ damaged voting paper"] <- "Against All"
Russia$affiliated_party[Russia$voted_parties=="RU - Communist Party of Russian Federation (Zyuganov G.)"] <- "Communist Party of Russian Federation"
Russia$affiliated_party[Russia$voted_parties=="RU - Liberal Democratic Party of Russia (Ghirinovsky V.)"] <- "Liberal Democratic Party of Russia"
Russia$affiliated_party[Russia$voted_parties=="RU - Missing"] <- "Missing Responses"
Russia$affiliated_party[Russia$voted_parties=="RU - United Russia (Gryzlov B.)"] <- "United Russia"
Russia$affiliated_party <- ifelse(is.na(Russia$affiliated_party), "Other Parties", Russia$affiliated_party)
table(Russia$affiliated_party, exclude = NULL)

Russia$affiliated_party <- factor(Russia$affiliated_party, levels = c("Missing Responses",
                                                                      "Against All", 
                                                                      "Communist Party of Russian Federation",
                                                                      "Liberal Democratic Party of Russia",
                                                                      "Other Parties"))
Russia$PARTY_LR_avg <- mean(Russia$PARTY_LR, na.rm = T)
Russia$PARTY_LR_sd <- sd(Russia$PARTY_LR, na.rm = T)
Russia$PARTY_LR_2sd <- Russia$PARTY_LR_sd*2
Russia$PARTY_LR_beta <- (Russia$PARTY_LR-Russia$PARTY_LR_avg)/Russia$PARTY_LR_2sd
table(Russia$PARTY_LR_beta, exclude = NULL)

Russia$religiosity_avg <- mean(Russia$religiosity, na.rm = T)
Russia$religiosity_sd <- sd(Russia$religiosity, na.rm = T)
Russia$religiosity_2sd <- Russia$religiosity_sd*2
Russia$religiosity_beta <- (Russia$religiosity-Russia$religiosity_avg)/Russia$religiosity_2sd
table(Russia$religiosity_beta, exclude = NULL)

table(Russia$urban_rural_area, exclude = NULL)
Russia$urban_rural_area_avg <- mean(Russia$urban_rural_area, na.rm = T)
Russia$urban_rural_area_sd <- sd(Russia$urban_rural_area, na.rm = T)
Russia$urban_rural_area_2sd <- Russia$urban_rural_area_sd*2
Russia$urban_rural_area_beta <- (Russia$urban_rural_area-Russia$urban_rural_area_avg)/Russia$urban_rural_area_2sd

table(Russia$age, exclude = NULL)
Russia$age_avg <- mean(Russia$age, na.rm = T)
Russia$age_sd <- sd(Russia$age, na.rm = T)
Russia$age_2sd <- Russia$age_sd*2
Russia$age_beta <- (Russia$age-Russia$age_avg)/Russia$age_2sd

table(Russia$degree, exclude = NULL)
Russia$degree_avg <- mean(Russia$degree, na.rm = T)
Russia$degree_sd <- sd(Russia$degree, na.rm = T)
Russia$degree_2sd <- Russia$degree_sd*2
Russia$degree_beta <- (Russia$degree-Russia$degree_avg)/Russia$degree_2sd

Russia$sex_dummy_avg <- mean(Russia$sex, na.rm = T)
Russia$sex_beta <- (Russia$sex-Russia$sex_dummy_avg)

Russia$political_interest_avg <- mean(Russia$political_interest, na.rm = T)
Russia$political_interest_sd <- sd(Russia$political_interest, na.rm = T)
Russia$political_interest_2sd <- Russia$political_interest_sd*2
Russia$political_interest_beta <- (Russia$political_interest-Russia$political_interest_avg)/Russia$political_interest_2sd

Russia$income_avg <- mean(Russia$income, na.rm = T)
Russia$income_sd <- sd(Russia$income, na.rm = T)
Russia$income_2sd <- Russia$income_sd*2
Russia$income_beta <- (Russia$income-Russia$income_avg)/Russia$income_2sd

library(nnet)

multinom_RU_m1 <- multinom(affiliated_party ~ PARTY_LR_beta + age_beta + degree_beta + income_beta + political_interest_beta + sex_beta + urban_rural_area_beta, data = Russia)
summary(multinom_RU_m1)
tab_model(multinom_RU_m1, show.ci = .95)

multinom_RU_m2 <- multinom(affiliated_party ~ religiosity_beta + age_beta + degree_beta + income_beta + political_interest_beta + sex_beta + urban_rural_area_beta, data = Russia)
summary(multinom_RU_m2)
tab_model(multinom_RU_m2, show.ci = .95)

#### Online Appendices, Table 14: Multinomial modeling of party affiliations in Russia ####

stargazer(multinom_RU_m1, multinom_RU_m2,
          type = "html", title=" ", digits=2, out="Tables/affiliation_models_ru.htm",
          model.numbers = F,
          covariate.labels = c("Left-Right Ideological Placement",
                               "Religious Attendance",
                               "Age",
                               "Education",
                               "Income",
                               "Level on Interest in Politics",
                               "Sex: Male (Base: Female)",
                               "Urban-Rural Area of Residence", 
                               "Intercept"))

library(AER)
coeftest(multinom_RU_m1)
coeftest(multinom_RU_m2)

round(p.adjust(c(2.20E-16,
                 0.2469554,
                 1.40E-07,
                 2.80E-08,
                 4.43E-01,
                 1.12E-03,
                 5.81E-01,
                 6.27E-01), method = "BH", n = length(c(2.20E-16,
                                                        0.2469554,
                                                        1.40E-07,
                                                        2.80E-08,
                                                        4.43E-01,
                                                        1.12E-03,
                                                        5.81E-01,
                                                        6.27E-01))), 3)

#### ADDITIONS AFTER REVISIONS ####

table(issp_2007$day_time_duration, exclude = NULL)
issp_2007$day_time_duration_avg <- mean(issp_2007$day_time_duration, na.rm = T)
issp_2007$day_time_duration_sd <- sd(issp_2007$day_time_duration, na.rm = T)
issp_2007$day_time_duration_2sd <- issp_2007$day_time_duration_sd*2
issp_2007$day_time_duration_beta <- (issp_2007$day_time_duration-issp_2007$day_time_duration_avg)/issp_2007$day_time_duration_2sd

contrast_m1_LR <- lmer(PARTY_LR_beta ~ chronotype_interval_beta*day_time_duration_beta + urban_rural_area_beta*day_time_duration_beta +
                         sex_beta + age_beta + degree_beta + income_beta + religiosity_beta + political_interest_beta +
                         (1 + chronotype_interval_beta + chronotype_interval_beta:day_time_duration_beta | country), 
                       data =  issp_2007)
summary(contrast_m1_LR)
tab_model(contrast_m1_LR, show.ci = 0.95)
round(variancePartition::calcVarPart(contrast_m1_LR)*100,2)
lmerTest::ranova(contrast_m1_LR)

library(optimx)
library(parallel)
library(minqa)

ncores <- detectCores()
diff_optims <- allFit(contrast_m1_LR, maxfun = 1e5, parallel = 'multicore', ncpus = ncores)
is.OK <- sapply(diff_optims, is, "merMod")
diff_optims.OK <- diff_optims[is.OK]
lapply(diff_optims.OK,function(x) x@optinfo$conv$lme4$messages)

contrast_m1_LR <- lmer(PARTY_LR_beta ~ chronotype_interval_beta*day_time_duration_beta + urban_rural_area_beta*day_time_duration_beta +
                         sex_beta + age_beta + degree_beta + income_beta + religiosity_beta + political_interest_beta +
                         (1 + chronotype_interval_beta + chronotype_interval_beta:day_time_duration_beta | country), 
                       data =  issp_2007,
                       control = lmerControl(optimizer = "nmkbw"))
summary(contrast_m1_LR)
tab_model(contrast_m1_LR, show.ci = 0.95)
round(variancePartition::calcVarPart(contrast_m1_LR)*100,2)
lmerTest::ranova(contrast_m1_LR)

coef(contrast_m1_LR)
arm::se.coef(contrast_m1_LR)

#### Table 15: Multilevel Linear Modeling of Left-Right Ideological Placement Additionally Accounting for Day Length’s Potential Moderation Effect ####

stargazer(contrast_m1_LR,
          type = "html", title=" ", digits=2, out="Tables/models_issp_2007_contrast.htm",
          model.numbers = F,
          column.labels = c("Model 1"),
          covariate.labels = c("Chronotype", 
                               "Day Length",
                               "Urban-Rural Area of Residence", 
                               "Sex: Male (Base: Female)",
                               "Age", "Education", "Income", "Religious Attendance", 
                               "Level on Interest in Politics", 
                               "Chronotype x Day Length",
                               "Urban-Rural Area of Residence x Day Length",
                               "Intercept"))

#### Preparation Online Appendices, Table 16: Predicted and Random Effects of Chronotype and Chronotype x Day Length Interaction Term on Left-Right Ideological Placement for Each Country by Additionally Accounting for Day Length’s Potential Moderation Effect ####

coef(contrast_m1_LR)
arm::se.coef(contrast_m1_LR)

ranef(contrast_m1_LR)
arm::se.ranef(contrast_m1_LR)

coef(contrast_m1_LR)$country
coef_contrast_m1_LR <- data.frame(coef(contrast_m1_LR)$country)
coef_contrast_m1_LR <- subset(coef_contrast_m1_LR, select = c("chronotype_interval_beta", "chronotype_interval_beta.day_time_duration_beta"))
coef_contrast_m1_LR$country <- row.names(coef_contrast_m1_LR)
names(coef_contrast_m1_LR)[names(coef_contrast_m1_LR)=="chronotype_interval_beta"] <- "coef_chronotype_interval_beta"
names(coef_contrast_m1_LR)[names(coef_contrast_m1_LR)=="chronotype_interval_beta.day_time_duration_beta"] <- "coef_chronotype_interval_beta.day_time_duration_beta"


se_coef_contrast_m1_LR <- data.frame(arm::se.coef(contrast_m1_LR)$country)
se_coef_contrast_m1_LR <- subset(se_coef_contrast_m1_LR, select = c("chronotype_interval_beta", "chronotype_interval_beta.day_time_duration_beta"))
se_coef_contrast_m1_LR$country <- row.names(se_coef_contrast_m1_LR)
names(se_coef_contrast_m1_LR)[names(se_coef_contrast_m1_LR)=="chronotype_interval_beta"] <- "se_chronotype_interval_beta"
names(se_coef_contrast_m1_LR)[names(se_coef_contrast_m1_LR)=="chronotype_interval_beta.day_time_duration_beta"] <- "se_chronotype_interval_beta.day_time_duration_beta"

coef_se_contrast_m1_LR <- merge(coef_contrast_m1_LR, se_coef_contrast_m1_LR, by="country")

coef_se_contrast_m1_LR$chrono.cl.90 <- coef_se_contrast_m1_LR$coef_chronotype_interval_beta - 1.645*coef_se_contrast_m1_LR$se_chronotype_interval_beta
coef_se_contrast_m1_LR$chrono.cu.90 <- coef_se_contrast_m1_LR$coef_chronotype_interval_beta + 1.645*coef_se_contrast_m1_LR$se_chronotype_interval_beta
coef_se_contrast_m1_LR$chrono.cl.95 <- coef_se_contrast_m1_LR$coef_chronotype_interval_beta - 1.96*coef_se_contrast_m1_LR$se_chronotype_interval_beta
coef_se_contrast_m1_LR$chrono.cu.95 <- coef_se_contrast_m1_LR$coef_chronotype_interval_beta + 1.96*coef_se_contrast_m1_LR$se_chronotype_interval_beta
coef_se_contrast_m1_LR$chrono.cl.99 <- coef_se_contrast_m1_LR$coef_chronotype_interval_beta - 2.576*coef_se_contrast_m1_LR$se_chronotype_interval_beta
coef_se_contrast_m1_LR$chrono.cu.99 <- coef_se_contrast_m1_LR$coef_chronotype_interval_beta + 2.576*coef_se_contrast_m1_LR$se_chronotype_interval_beta

coef_se_contrast_m1_LR$interaction.cl.90 <- coef_se_contrast_m1_LR$coef_chronotype_interval_beta.day_time_duration_beta - 1.645*coef_se_contrast_m1_LR$se_chronotype_interval_beta.day_time_duration_beta
coef_se_contrast_m1_LR$interaction.cu.90 <- coef_se_contrast_m1_LR$coef_chronotype_interval_beta.day_time_duration_beta + 1.645*coef_se_contrast_m1_LR$se_chronotype_interval_beta.day_time_duration_beta
coef_se_contrast_m1_LR$interaction.cl.95 <- coef_se_contrast_m1_LR$coef_chronotype_interval_beta.day_time_duration_beta - 1.96*coef_se_contrast_m1_LR$se_chronotype_interval_beta.day_time_duration_beta
coef_se_contrast_m1_LR$interaction.cu.95 <- coef_se_contrast_m1_LR$coef_chronotype_interval_beta.day_time_duration_beta + 1.96*coef_se_contrast_m1_LR$se_chronotype_interval_beta.day_time_duration_beta
coef_se_contrast_m1_LR$interaction.cl.99 <- coef_se_contrast_m1_LR$coef_chronotype_interval_beta.day_time_duration_beta - 2.576*coef_se_contrast_m1_LR$se_chronotype_interval_beta.day_time_duration_beta
coef_se_contrast_m1_LR$interaction.cu.99 <- coef_se_contrast_m1_LR$coef_chronotype_interval_beta.day_time_duration_beta + 2.576*coef_se_contrast_m1_LR$se_chronotype_interval_beta.day_time_duration_beta

View(coef_se_contrast_m1_LR)

ranef(contrast_m1_LR)$country
ranef_contrast_m1_LR <- data.frame(ranef(contrast_m1_LR)$country)
ranef_contrast_m1_LR <- subset(ranef_contrast_m1_LR, select = c("chronotype_interval_beta", "chronotype_interval_beta.day_time_duration_beta"))
ranef_contrast_m1_LR$country <- row.names(ranef_contrast_m1_LR)
names(ranef_contrast_m1_LR)[names(ranef_contrast_m1_LR)=="chronotype_interval_beta"] <- "ranef_chronotype_interval_beta"
names(ranef_contrast_m1_LR)[names(ranef_contrast_m1_LR)=="chronotype_interval_beta.day_time_duration_beta"] <- "ranef_chronotype_interval_beta.day_time_duration_beta"

se_ranef_contrast_m1_LR <- data.frame(arm::se.ranef(contrast_m1_LR)$country)
se_ranef_contrast_m1_LR <- subset(se_ranef_contrast_m1_LR, select = c("chronotype_interval_beta", "chronotype_interval_beta.day_time_duration_beta"))
se_ranef_contrast_m1_LR$country <- row.names(se_ranef_contrast_m1_LR)
names(se_ranef_contrast_m1_LR)[names(se_ranef_contrast_m1_LR)=="chronotype_interval_beta"] <- "se_chronotype_interval_beta"
names(se_ranef_contrast_m1_LR)[names(se_ranef_contrast_m1_LR)=="chronotype_interval_beta.day_time_duration_beta"] <- "se_chronotype_interval_beta.day_time_duration_beta"

ranef_se_contrast_m1_LR <- merge(ranef_contrast_m1_LR, se_ranef_contrast_m1_LR, by="country")

ranef_se_contrast_m1_LR$chrono.cl.90 <- ranef_se_contrast_m1_LR$ranef_chronotype_interval_beta - 1.645*ranef_se_contrast_m1_LR$se_chronotype_interval_beta
ranef_se_contrast_m1_LR$chrono.cu.90 <- ranef_se_contrast_m1_LR$ranef_chronotype_interval_beta + 1.645*ranef_se_contrast_m1_LR$se_chronotype_interval_beta
ranef_se_contrast_m1_LR$chrono.cl.95 <- ranef_se_contrast_m1_LR$ranef_chronotype_interval_beta - 1.96*ranef_se_contrast_m1_LR$se_chronotype_interval_beta
ranef_se_contrast_m1_LR$chrono.cu.95 <- ranef_se_contrast_m1_LR$ranef_chronotype_interval_beta + 1.96*ranef_se_contrast_m1_LR$se_chronotype_interval_beta
ranef_se_contrast_m1_LR$chrono.cl.99 <- ranef_se_contrast_m1_LR$ranef_chronotype_interval_beta - 2.576*ranef_se_contrast_m1_LR$se_chronotype_interval_beta
ranef_se_contrast_m1_LR$chrono.cu.99 <- ranef_se_contrast_m1_LR$ranef_chronotype_interval_beta + 2.576*ranef_se_contrast_m1_LR$se_chronotype_interval_beta

ranef_se_contrast_m1_LR$interaction.cl.90 <- ranef_se_contrast_m1_LR$ranef_chronotype_interval_beta.day_time_duration_beta - 1.645*ranef_se_contrast_m1_LR$se_chronotype_interval_beta.day_time_duration_beta
ranef_se_contrast_m1_LR$interaction.cu.90 <- ranef_se_contrast_m1_LR$ranef_chronotype_interval_beta.day_time_duration_beta + 1.645*ranef_se_contrast_m1_LR$se_chronotype_interval_beta.day_time_duration_beta
ranef_se_contrast_m1_LR$interaction.cl.95 <- ranef_se_contrast_m1_LR$ranef_chronotype_interval_beta.day_time_duration_beta - 1.96*ranef_se_contrast_m1_LR$se_chronotype_interval_beta.day_time_duration_beta
ranef_se_contrast_m1_LR$interaction.cu.95 <- ranef_se_contrast_m1_LR$ranef_chronotype_interval_beta.day_time_duration_beta + 1.96*ranef_se_contrast_m1_LR$se_chronotype_interval_beta.day_time_duration_beta
ranef_se_contrast_m1_LR$interaction.cl.99 <- ranef_se_contrast_m1_LR$ranef_chronotype_interval_beta.day_time_duration_beta - 2.576*ranef_se_contrast_m1_LR$se_chronotype_interval_beta.day_time_duration_beta
ranef_se_contrast_m1_LR$interaction.cu.99 <- ranef_se_contrast_m1_LR$ranef_chronotype_interval_beta.day_time_duration_beta + 2.576*ranef_se_contrast_m1_LR$se_chronotype_interval_beta.day_time_duration_beta

View(ranef_se_contrast_m1_LR)

#### Online Appendices, Table 17: Average Marginal Effects of Chronotype’s Main Effect for Each Country ####


library(marginaleffects)

summary(marginaleffects(contrast_m1_LR, variables = c("chronotype_interval_beta"), 
                        newdata = datagrid(day_time_duration_beta = 1, 
                                           urban_rural_area_beta = 0,
                                           sex_beta = 0,
                                           age_beta = 0,
                                           degree_beta = 0,
                                           income_beta = 0,
                                           religiosity_beta = 0,
                                           political_interest_beta = 0,
                                           country = "Finland")))

summary(marginaleffects(contrast_m1_LR, variables = c("chronotype_interval_beta"), 
                        newdata = datagrid(day_time_duration_beta = 1, 
                                           urban_rural_area_beta = 0,
                                           sex_beta = 0,
                                           age_beta = 0,
                                           degree_beta = 0,
                                           income_beta = 0,
                                           religiosity_beta = 0,
                                           political_interest_beta = 0,
                                           country = "Ireland")))

summary(marginaleffects(contrast_m1_LR, variables = c("chronotype_interval_beta"), 
                        newdata = datagrid(day_time_duration_beta = 1, 
                                           urban_rural_area_beta = 0,
                                           sex_beta = 0,
                                           age_beta = 0,
                                           degree_beta = 0,
                                           income_beta = 0,
                                           religiosity_beta = 0,
                                           political_interest_beta = 0,
                                           country = "Mexico")))

summary(marginaleffects(contrast_m1_LR, variables = c("chronotype_interval_beta"), 
                        newdata = datagrid(day_time_duration_beta = 1, 
                                           urban_rural_area_beta = 0,
                                           sex_beta = 0,
                                           age_beta = 0,
                                           degree_beta = 0,
                                           income_beta = 0,
                                           religiosity_beta = 0,
                                           political_interest_beta = 0,
                                           country = "the Netherlands")))

summary(marginaleffects(contrast_m1_LR, variables = c("chronotype_interval_beta"), 
                        newdata = datagrid(day_time_duration_beta = 1, 
                                           urban_rural_area_beta = 0,
                                           sex_beta = 0,
                                           age_beta = 0,
                                           degree_beta = 0,
                                           income_beta = 0,
                                           religiosity_beta = 0,
                                           political_interest_beta = 0,
                                           country = "New Zealand")))

summary(marginaleffects(contrast_m1_LR, variables = c("chronotype_interval_beta"), 
                        newdata = datagrid(day_time_duration_beta = 1, 
                                           urban_rural_area_beta = 0,
                                           sex_beta = 0,
                                           age_beta = 0,
                                           degree_beta = 0,
                                           income_beta = 0,
                                           religiosity_beta = 0,
                                           political_interest_beta = 0,
                                           country = "the Philippines")))

summary(marginaleffects(contrast_m1_LR, variables = c("chronotype_interval_beta"), 
                        newdata = datagrid(day_time_duration_beta = 1,
                                           urban_rural_area_beta = 0,
                                           sex_beta = 0,
                                           age_beta = 0,
                                           degree_beta = 0,
                                           income_beta = 0,
                                           religiosity_beta = 0,
                                           political_interest_beta = 0,
                                           country = "Russia")))

summary(marginaleffects(contrast_m1_LR, variables = c("chronotype_interval_beta"), 
                        newdata = datagrid(day_time_duration_beta = 1, 
                                           urban_rural_area_beta = 0,
                                           sex_beta = 0,
                                           age_beta = 0,
                                           degree_beta = 0,
                                           income_beta = 0,
                                           religiosity_beta = 0,
                                           political_interest_beta = 0,
                                           country = "South Korea")))

summary(marginaleffects(contrast_m1_LR, variables = c("chronotype_interval_beta"), 
                        newdata = datagrid(day_time_duration_beta = 1,
                                           urban_rural_area_beta = 0,
                                           sex_beta = 0,
                                           age_beta = 0,
                                           degree_beta = 0,
                                           income_beta = 0,
                                           religiosity_beta = 0,
                                           political_interest_beta = 0,
                                           country = "Switzerland")))

#### Country Level Controls ####

table(issp_2007$country, exclude = NULL)

issp_2007$dinner_time <- NA
issp_2007$dinner_time[issp_2007$country=="Finland"] <- 17
issp_2007$dinner_time[issp_2007$country=="New Zealand"] <- 18.15
issp_2007$dinner_time[issp_2007$country=="the Philippines"] <- 18.45
issp_2007$dinner_time[issp_2007$country=="Ireland" | issp_2007$country=="the Netherlands"] <- 19
issp_2007$dinner_time[issp_2007$country=="South Korea"] <- 19.15
issp_2007$dinner_time[issp_2007$country=="Russia"] <- 19.30
issp_2007$dinner_time[issp_2007$country=="Switzerland"] <- 19.45
issp_2007$dinner_time[issp_2007$country=="Mexico"] <- 20
issp_2007$dinner_time[issp_2007$country=="Greece"] <- 21.30

table(issp_2007$dinner_time, exclude = NULL)
issp_2007$dinner_time_avg <- mean(issp_2007$dinner_time, na.rm = T)
issp_2007$dinner_time_sd <- sd(issp_2007$dinner_time, na.rm = T)
issp_2007$dinner_time_2sd <- issp_2007$dinner_time_sd*2
issp_2007$dinner_time_beta <- (issp_2007$dinner_time-issp_2007$dinner_time_avg)/issp_2007$dinner_time_2sd
table(issp_2007$dinner_time_beta, exclude = NULL)

issp_2007$predominant_denomination <- NA
issp_2007$predominant_denomination[issp_2007$country=="Finland" | 
                                     issp_2007$country=="New Zealand"] <- "Protestant"
issp_2007$predominant_denomination[issp_2007$country=="Ireland" |
                                     issp_2007$country=="Mexico" | 
                                     issp_2007$country=="the Philippines" |
                                     issp_2007$country=="Switzerland"] <- "Roman Catholic"
issp_2007$predominant_denomination[issp_2007$country=="the Netherlands"] <- "None"
issp_2007$predominant_denomination[issp_2007$country=="Greece" | issp_2007$country=="Russia"] <- "Orthodox"
issp_2007$predominant_denomination[issp_2007$country=="South Korea"] <- "Buddhist"
table(issp_2007$predominant_denomination, exclude = NULL)
issp_2007$predominant_denomination <- factor(issp_2007$predominant_denomination, levels = c("Roman Catholic", "Buddhist", "None", "Orthodox", "Protestant"))

issp_2007$predominant_Buddhist <- NA
issp_2007$predominant_Buddhist[issp_2007$predominant_denomination=="Buddhist"] <- 1
issp_2007$predominant_Buddhist <- ifelse(is.na(issp_2007$predominant_Buddhist), 0, issp_2007$predominant_Buddhist)
table(issp_2007$predominant_Buddhist, exclude = NULL)

issp_2007$predominant_Buddhist_avg <- mean(issp_2007$predominant_Buddhist, na.rm = T)
issp_2007$predominant_Buddhist_beta <- (issp_2007$predominant_Buddhist-issp_2007$predominant_Buddhist_avg)

issp_2007$predominant_None <- NA
issp_2007$predominant_None[issp_2007$predominant_denomination=="None"] <- 1
issp_2007$predominant_None <- ifelse(is.na(issp_2007$predominant_None), 0, issp_2007$predominant_None)
table(issp_2007$predominant_None, exclude = NULL)

issp_2007$predominant_None_avg <- mean(issp_2007$predominant_None, na.rm = T)
issp_2007$predominant_None_beta <- (issp_2007$predominant_None-issp_2007$predominant_None_avg)

issp_2007$predominant_Orthodox <- NA
issp_2007$predominant_Orthodox[issp_2007$predominant_denomination=="Orthodox"] <- 1
issp_2007$predominant_Orthodox <- ifelse(is.na(issp_2007$predominant_Orthodox), 0, issp_2007$predominant_Orthodox)
table(issp_2007$predominant_Orthodox, exclude = NULL)

issp_2007$predominant_Orthodox_avg <- mean(issp_2007$predominant_Orthodox, na.rm = T)
issp_2007$predominant_Orthodox_beta <- (issp_2007$predominant_Orthodox-issp_2007$predominant_Orthodox_avg)

issp_2007$predominant_Protestant <- NA
issp_2007$predominant_Protestant[issp_2007$predominant_denomination=="Protestant"] <- 1
issp_2007$predominant_Protestant <- ifelse(is.na(issp_2007$predominant_Protestant), 0, issp_2007$predominant_Protestant)
table(issp_2007$predominant_Protestant, exclude = NULL)

issp_2007$predominant_Protestant_avg <- mean(issp_2007$predominant_Protestant, na.rm = T)
issp_2007$predominant_Protestant_beta <- (issp_2007$predominant_Protestant-issp_2007$predominant_Protestant_avg)

issp_2007$predominant_Catholic <- NA
issp_2007$predominant_Catholic[issp_2007$predominant_denomination=="Roman Catholic"] <- 1
issp_2007$predominant_Catholic <- ifelse(is.na(issp_2007$predominant_Catholic), 0, issp_2007$predominant_Catholic)
table(issp_2007$predominant_Catholic, exclude = NULL)

issp_2007$predominant_Catholic_avg <- mean(issp_2007$predominant_Catholic, na.rm = T)
issp_2007$predominant_Catholic_beta <- (issp_2007$predominant_Catholic-issp_2007$predominant_Catholic_avg)

issp_2007$gini_index <- NA
issp_2007$gini_index[issp_2007$country=="Finland"] <- 28.3
issp_2007$gini_index[issp_2007$country=="Ireland"] <- 30.9
issp_2007$gini_index[issp_2007$country=="Greece"] <- 32.9
issp_2007$gini_index[issp_2007$country=="Mexico"] <- 49.9
issp_2007$gini_index[issp_2007$country=="the Netherlands"] <- 29.3
issp_2007$gini_index[issp_2007$country=="New Zealand"] <- 37.3
issp_2007$gini_index[issp_2007$country=="the Philippines"] <- 47.2
issp_2007$gini_index[issp_2007$country=="Russia"] <- 42.3
issp_2007$gini_index[issp_2007$country=="South Korea"] <- 31.7 
issp_2007$gini_index[issp_2007$country=="Switzerland"] <- 34.3

table(issp_2007$gini_index, exclude = NULL)
issp_2007$gini_index_avg <- mean(issp_2007$gini_index, na.rm = T)
issp_2007$gini_index_sd <- sd(issp_2007$gini_index, na.rm = T)
issp_2007$gini_index_2sd <- issp_2007$gini_index_sd*2
issp_2007$gini_index_beta <- (issp_2007$gini_index-issp_2007$gini_index_avg)/issp_2007$gini_index_2sd

issp_2007$enep <- NA
issp_2007$enep[issp_2007$country=="Finland"] <- 4.93
issp_2007$enep[issp_2007$country=="Ireland"] <- 3.03
issp_2007$enep[issp_2007$country=="Greece"] <- 2.71
issp_2007$enep[issp_2007$country=="Mexico"] <- 3.03
issp_2007$enep[issp_2007$country=="the Netherlands"] <- 5.54 
issp_2007$enep[issp_2007$country=="New Zealand"] <- 2.98
issp_2007$enep[issp_2007$country=="the Philippines"] <- 1.7
issp_2007$enep[issp_2007$country=="Russia"] <- 3.60
issp_2007$enep[issp_2007$country=="South Korea"] <- 2.36 
issp_2007$enep[issp_2007$country=="Switzerland"] <- 5.01

table(issp_2007$enep, exclude = NULL)
issp_2007$enep_avg <- mean(issp_2007$enep, na.rm = T)
issp_2007$enep_sd <- sd(issp_2007$enep, na.rm = T)
issp_2007$enep_2sd <- issp_2007$enep_sd*2
issp_2007$enep_beta <- (issp_2007$enep-issp_2007$enep_avg)/issp_2007$enep_2sd

issp_2007$protein_animal <- NA
issp_2007$protein_animal[issp_2007$country=="Finland"] <- 66.70
issp_2007$protein_animal[issp_2007$country=="Ireland"] <- 55.30
issp_2007$protein_animal[issp_2007$country=="Greece"] <- 67.60
issp_2007$protein_animal[issp_2007$country=="Mexico"] <- 40.30
issp_2007$protein_animal[issp_2007$country=="the Netherlands"] <- 73 
issp_2007$protein_animal[issp_2007$country=="New Zealand"] <- 57.70
issp_2007$protein_animal[issp_2007$country=="the Philippines"] <- 25
issp_2007$protein_animal[issp_2007$country=="Russia"] <- 50.30
issp_2007$protein_animal[issp_2007$country=="South Korea"] <- 42
issp_2007$protein_animal[issp_2007$country=="Switzerland"] <- 58.7

table(issp_2007$protein_animal, exclude = NULL)
issp_2007$protein_animal_avg <- mean(issp_2007$protein_animal, na.rm = T)
issp_2007$protein_animal_sd <- sd(issp_2007$protein_animal, na.rm = T)
issp_2007$protein_animal_2sd <- issp_2007$protein_animal_sd*2
issp_2007$protein_animal_beta <- (issp_2007$protein_animal-issp_2007$protein_animal_avg)/issp_2007$protein_animal_2sd

macro_m1_LR <- lmer(PARTY_LR_beta ~ chronotype_interval_beta + urban_rural_area_beta +
                      sex_beta + age_beta + degree_beta + income_beta + religiosity_beta + political_interest_beta +
                      protein_animal_beta +   
                      dinner_time_beta + 
                      predominant_Buddhist_beta +
                      predominant_None_beta +
                      predominant_Orthodox_beta + 
                      predominant_Protestant_beta +
                      gini_index_beta + 
                      enep_beta +
                      (1 + chronotype_interval_beta | country) +
                      (0 + chronotype_interval_beta | day_off), 
                    data =  issp_2007)
summary(macro_m1_LR)
tab_model(macro_m1_LR, show.ci = 0.95)
round(variancePartition::calcVarPart(macro_m1_LR)*100,2)
lmerTest::ranova(macro_m1_LR)


coef(macro_m1_LR)
arm::se.coef(macro_m1_LR)

library(optimx)
library(parallel)
library(minqa)

ncores <- detectCores()
diff_optims <- allFit(macro_m1_LR, maxfun = 1e5, parallel = 'multicore', ncpus = ncores)
is.OK <- sapply(diff_optims, is, "merMod")
diff_optims.OK <- diff_optims[is.OK]
lapply(diff_optims.OK,function(x) x@optinfo$conv$lme4$messages)

macro_m1_LR <- lmer(PARTY_LR_beta ~ chronotype_interval_beta + urban_rural_area_beta +
                      sex_beta + age_beta + degree_beta + income_beta + religiosity_beta + political_interest_beta +
                      protein_animal_beta +   
                      dinner_time_beta + 
                      predominant_Buddhist_beta +
                      predominant_None_beta +
                      predominant_Orthodox_beta + 
                      predominant_Protestant_beta +
                      gini_index_beta + 
                      enep_beta +
                      (1 + chronotype_interval_beta | country) +
                      (0 + chronotype_interval_beta | day_off), 
                    data =  issp_2007,
                    control = lmerControl(optimizer = "nloptwrap",
                                          optCtrl = list(algorithm = "NLOPT_LN_NELDERMEAD", maxit = 1e6)))
summary(macro_m1_LR)
tab_model(macro_m1_LR, show.ci = 0.95)
round(variancePartition::calcVarPart(macro_m1_LR)*100,2)
lmerTest::ranova(macro_m1_LR)

coef(macro_m1_LR)
arm::se.coef(macro_m1_LR)

table(issp_2007$country, issp_2007$mid_field_season, exclude = NULL)

macro_m1_RELIG <- lmer(religiosity_beta ~ chronotype_interval_beta + urban_rural_area_beta +
                         sex_beta + age_beta + degree_beta + income_beta + PARTY_LR_beta + political_interest_beta +
                         protein_animal_beta +   
                         dinner_time_beta + 
                         predominant_Buddhist_beta +
                         predominant_None_beta +
                         predominant_Orthodox_beta + 
                         predominant_Protestant_beta +
                         gini_index_beta + 
                         enep_beta +
                         (1 + chronotype_interval_beta | country) +
                         (0 + chronotype_interval_beta | day_off), 
                       data =  issp_2007)
summary(macro_m1_RELIG)

library(optimx)
library(parallel)
library(minqa)

ncores <- detectCores()
diff_optims <- allFit(macro_m1_RELIG, maxfun = 1e5, parallel = 'multicore', ncpus = ncores)
is.OK <- sapply(diff_optims, is, "merMod")
diff_optims.OK <- diff_optims[is.OK]
lapply(diff_optims.OK,function(x) x@optinfo$conv$lme4$messages)

#### Table 18. Multilevel Linear Modeling of Left-Right Ideological Placement by Additionally Controlling Country-Level Covariates ####


stargazer(macro_m1_LR,
          type = "html", title=" ", digits=2, out="Tables/models_issp_2007_macro.htm",
          model.numbers = F,
          column.labels = c("Model 1"),
          covariate.labels = c("Chronotype", 
                               "Urban-Rural Area of Residence", 
                               "Sex: Male (Base: Female)",
                               "Age", 
                               "Education",
                               "Income",
                               "Religious Attendance",
                               "Left-Right Ideological Placement", 
                               "Level on Interest in Politics", 
                               "Average Supply of Protein of Animal Origin",
                               "Average Dinner Time",
                               "Predominant Denomination: Buddhist (Base: Roman Catholic)",
                               "Predominant Denomination: None (Base: Roman Catholic)",
                               "Predominant Denomination: Orthodox (Base: Roman Catholic)",
                               "Predominant Denomination: Protestant (Base: Roman Catholic)",
                               "GINI Index",
                               "Effective Number of Legislative Parties",
                               "Intercept"))

tab_model(macro_m1_LR, show.ci = 0.95)

#### Preparation Online Appendices, Table 19. Predicted and Random Effects of Chronotype on Left-Right Ideological Placement and Religious Attendance for Each Country by Additionally Controlling Country-Level Covariates ####

coef(macro_m1_LR)
arm::se.coef(macro_m1_LR)

ranef(macro_m1_LR)
arm::se.ranef(macro_m1_LR)

coef(macro_m1_LR)$country
coef_macro_m1_LR <- data.frame(coef(macro_m1_LR)$country)
coef_macro_m1_LR <- subset(coef_macro_m1_LR, select = c("chronotype_interval_beta"))
coef_macro_m1_LR$country <- row.names(coef_macro_m1_LR)
names(coef_macro_m1_LR)[names(coef_macro_m1_LR)=="chronotype_interval_beta"] <- "coef_chronotype_interval_beta"


se_coef_macro_m1_LR <- data.frame(arm::se.coef(macro_m1_LR)$country)
se_coef_macro_m1_LR <- subset(se_coef_macro_m1_LR, select = c("chronotype_interval_beta"))
se_coef_macro_m1_LR$country <- row.names(se_coef_macro_m1_LR)
names(se_coef_macro_m1_LR)[names(se_coef_macro_m1_LR)=="chronotype_interval_beta"] <- "se_chronotype_interval_beta"

coef_se_macro_m1_LR <- merge(coef_macro_m1_LR, se_coef_macro_m1_LR, by="country")

coef_se_macro_m1_LR$chrono.cl.90 <- coef_se_macro_m1_LR$coef_chronotype_interval_beta - 1.645*coef_se_macro_m1_LR$se_chronotype_interval_beta
coef_se_macro_m1_LR$chrono.cu.90 <- coef_se_macro_m1_LR$coef_chronotype_interval_beta + 1.645*coef_se_macro_m1_LR$se_chronotype_interval_beta
coef_se_macro_m1_LR$chrono.cl.95 <- coef_se_macro_m1_LR$coef_chronotype_interval_beta - 1.96*coef_se_macro_m1_LR$se_chronotype_interval_beta
coef_se_macro_m1_LR$chrono.cu.95 <- coef_se_macro_m1_LR$coef_chronotype_interval_beta + 1.96*coef_se_macro_m1_LR$se_chronotype_interval_beta
coef_se_macro_m1_LR$chrono.cl.99 <- coef_se_macro_m1_LR$coef_chronotype_interval_beta - 2.576*coef_se_macro_m1_LR$se_chronotype_interval_beta
coef_se_macro_m1_LR$chrono.cu.99 <- coef_se_macro_m1_LR$coef_chronotype_interval_beta + 2.576*coef_se_macro_m1_LR$se_chronotype_interval_beta

View(coef_se_macro_m1_LR)

ranef(macro_m1_LR)$country
ranef_macro_m1_LR <- data.frame(ranef(macro_m1_LR)$country)
ranef_macro_m1_LR <- subset(ranef_macro_m1_LR, select = c("chronotype_interval_beta"))
ranef_macro_m1_LR$country <- row.names(ranef_macro_m1_LR)
names(ranef_macro_m1_LR)[names(ranef_macro_m1_LR)=="chronotype_interval_beta"] <- "ranef_chronotype_interval_beta"


se_ranef_macro_m1_LR <- data.frame(arm::se.ranef(macro_m1_LR)$country)
se_ranef_macro_m1_LR <- subset(se_ranef_macro_m1_LR, select = c("chronotype_interval_beta"))
se_ranef_macro_m1_LR$country <- row.names(se_ranef_macro_m1_LR)
names(se_ranef_macro_m1_LR)[names(se_ranef_macro_m1_LR)=="chronotype_interval_beta"] <- "se_chronotype_interval_beta"

ranef_se_macro_m1_LR <- merge(ranef_macro_m1_LR, se_ranef_macro_m1_LR, by="country")

ranef_se_macro_m1_LR$chrono.cl.90 <- ranef_se_macro_m1_LR$ranef_chronotype_interval_beta - 1.645*ranef_se_macro_m1_LR$se_chronotype_interval_beta
ranef_se_macro_m1_LR$chrono.cu.90 <- ranef_se_macro_m1_LR$ranef_chronotype_interval_beta + 1.645*ranef_se_macro_m1_LR$se_chronotype_interval_beta
ranef_se_macro_m1_LR$chrono.cl.95 <- ranef_se_macro_m1_LR$ranef_chronotype_interval_beta - 1.96*ranef_se_macro_m1_LR$se_chronotype_interval_beta
ranef_se_macro_m1_LR$chrono.cu.95 <- ranef_se_macro_m1_LR$ranef_chronotype_interval_beta + 1.96*ranef_se_macro_m1_LR$se_chronotype_interval_beta
ranef_se_macro_m1_LR$chrono.cl.99 <- ranef_se_macro_m1_LR$ranef_chronotype_interval_beta - 2.576*ranef_se_macro_m1_LR$se_chronotype_interval_beta
ranef_se_macro_m1_LR$chrono.cu.99 <- ranef_se_macro_m1_LR$ranef_chronotype_interval_beta + 2.576*ranef_se_macro_m1_LR$se_chronotype_interval_beta

View(ranef_se_macro_m1_LR)

#### Online Appendices, Table 26: Multilevel Linear Modeling of Religious Attendance Additionally Accounting for Urban-Rural Area of Residence's Potential Moderation Effect ####

issp_2007_religious_attendance_model3_moderation <- lmer(religiosity_beta ~ chronotype_interval_beta*urban_rural_area_beta +
                                                           sex_beta + age_beta + degree_beta + income_beta + PARTY_LR_beta + political_interest_beta + 
                                                           (1 + chronotype_interval_beta + chronotype_interval_beta:urban_rural_area_beta | country), 
                                                         data =  issp_2007)
summary(issp_2007_religious_attendance_model3_moderation)
tab_model(issp_2007_religious_attendance_model3_moderation, show.ci = 0.95)

stargazer(issp_2007_religious_attendance_model3_moderation,
          type = "html", title=" ", digits=2, out="Tables/models_issp_2007_urbanization_moderation.htm",
          model.numbers = F,
          column.labels = c("Model 1"),
          covariate.labels = c("Chronotype", 
                               "Urban-Rural Area of Residence", 
                               "Sex: Male (Base: Female)",
                               "Age", "Education", "Income", "Left-Right Ideological Placement", 
                               "Level on Interest in Politics", 
                               "Chronotype x Urban-Rural Area of Residence",
                               "Intercept"))

tab_model(issp_2007_religious_attendance_model3_moderation, show.ci = 0.95)

#### Preparation Online Appendices, Table 27: Predicted and Random Effects of Chronotype and Chronotype x Urban-Rural Area of Residence Interaction Term on Left-Right Ideological Placement for Each Country by Additionally Accounting for Urban-Rural Area of Residence’s Potential Moderation Effect ####

coef(issp_2007_religious_attendance_model3_moderation)
arm::se.coef(issp_2007_religious_attendance_model3_moderation)

coef(issp_2007_religious_attendance_model3_moderation)$country
coef_issp_2007_religious_attendance_model3_moderation <- data.frame(coef(issp_2007_religious_attendance_model3_moderation)$country)
coef_issp_2007_religious_attendance_model3_moderation <- subset(coef_issp_2007_religious_attendance_model3_moderation, select = c("chronotype_interval_beta", "chronotype_interval_beta.urban_rural_area_beta"))
coef_issp_2007_religious_attendance_model3_moderation$country <- row.names(coef_issp_2007_religious_attendance_model3_moderation)
names(coef_issp_2007_religious_attendance_model3_moderation)[names(coef_issp_2007_religious_attendance_model3_moderation)=="chronotype_interval_beta"] <- "coef_chronotype_interval_beta"
names(coef_issp_2007_religious_attendance_model3_moderation)[names(coef_issp_2007_religious_attendance_model3_moderation)=="chronotype_interval_beta.urban_rural_area_beta"] <- "coef_chronotype_interval_beta.urban_rural_area_beta"


se_coef_issp_2007_religious_attendance_model3_moderation <- data.frame(arm::se.coef(issp_2007_religious_attendance_model3_moderation)$country)
se_coef_issp_2007_religious_attendance_model3_moderation <- subset(se_coef_issp_2007_religious_attendance_model3_moderation, select = c("chronotype_interval_beta", "chronotype_interval_beta.urban_rural_area_beta"))
se_coef_issp_2007_religious_attendance_model3_moderation$country <- row.names(se_coef_issp_2007_religious_attendance_model3_moderation)
names(se_coef_issp_2007_religious_attendance_model3_moderation)[names(se_coef_issp_2007_religious_attendance_model3_moderation)=="chronotype_interval_beta"] <- "se_chronotype_interval_beta"
names(se_coef_issp_2007_religious_attendance_model3_moderation)[names(se_coef_issp_2007_religious_attendance_model3_moderation)=="chronotype_interval_beta.urban_rural_area_beta"] <- "se_chronotype_interval_beta.urban_rural_area_beta"

coef_se_issp_2007_religious_attendance_model3_moderation <- merge(coef_issp_2007_religious_attendance_model3_moderation, se_coef_issp_2007_religious_attendance_model3_moderation, by="country")

coef_se_issp_2007_religious_attendance_model3_moderation$chrono.cl.90 <- coef_se_issp_2007_religious_attendance_model3_moderation$coef_chronotype_interval_beta - 1.645*coef_se_issp_2007_religious_attendance_model3_moderation$se_chronotype_interval_beta
coef_se_issp_2007_religious_attendance_model3_moderation$chrono.cu.90 <- coef_se_issp_2007_religious_attendance_model3_moderation$coef_chronotype_interval_beta + 1.645*coef_se_issp_2007_religious_attendance_model3_moderation$se_chronotype_interval_beta
coef_se_issp_2007_religious_attendance_model3_moderation$chrono.cl.95 <- coef_se_issp_2007_religious_attendance_model3_moderation$coef_chronotype_interval_beta - 1.96*coef_se_issp_2007_religious_attendance_model3_moderation$se_chronotype_interval_beta
coef_se_issp_2007_religious_attendance_model3_moderation$chrono.cu.95 <- coef_se_issp_2007_religious_attendance_model3_moderation$coef_chronotype_interval_beta + 1.96*coef_se_issp_2007_religious_attendance_model3_moderation$se_chronotype_interval_beta
coef_se_issp_2007_religious_attendance_model3_moderation$chrono.cl.99 <- coef_se_issp_2007_religious_attendance_model3_moderation$coef_chronotype_interval_beta - 2.576*coef_se_issp_2007_religious_attendance_model3_moderation$se_chronotype_interval_beta
coef_se_issp_2007_religious_attendance_model3_moderation$chrono.cu.99 <- coef_se_issp_2007_religious_attendance_model3_moderation$coef_chronotype_interval_beta + 2.576*coef_se_issp_2007_religious_attendance_model3_moderation$se_chronotype_interval_beta

coef_se_issp_2007_religious_attendance_model3_moderation$interaction.cl.90 <- coef_se_issp_2007_religious_attendance_model3_moderation$coef_chronotype_interval_beta.urban_rural_area_beta - 1.645*coef_se_issp_2007_religious_attendance_model3_moderation$se_chronotype_interval_beta.urban_rural_area_beta
coef_se_issp_2007_religious_attendance_model3_moderation$interaction.cu.90 <- coef_se_issp_2007_religious_attendance_model3_moderation$coef_chronotype_interval_beta.urban_rural_area_beta + 1.645*coef_se_issp_2007_religious_attendance_model3_moderation$se_chronotype_interval_beta.urban_rural_area_beta
coef_se_issp_2007_religious_attendance_model3_moderation$interaction.cl.95 <- coef_se_issp_2007_religious_attendance_model3_moderation$coef_chronotype_interval_beta.urban_rural_area_beta - 1.96*coef_se_issp_2007_religious_attendance_model3_moderation$se_chronotype_interval_beta.urban_rural_area_beta
coef_se_issp_2007_religious_attendance_model3_moderation$interaction.cu.95 <- coef_se_issp_2007_religious_attendance_model3_moderation$coef_chronotype_interval_beta.urban_rural_area_beta + 1.96*coef_se_issp_2007_religious_attendance_model3_moderation$se_chronotype_interval_beta.urban_rural_area_beta
coef_se_issp_2007_religious_attendance_model3_moderation$interaction.cl.99 <- coef_se_issp_2007_religious_attendance_model3_moderation$coef_chronotype_interval_beta.urban_rural_area_beta - 2.576*coef_se_issp_2007_religious_attendance_model3_moderation$se_chronotype_interval_beta.urban_rural_area_beta
coef_se_issp_2007_religious_attendance_model3_moderation$interaction.cu.99 <- coef_se_issp_2007_religious_attendance_model3_moderation$coef_chronotype_interval_beta.urban_rural_area_beta + 2.576*coef_se_issp_2007_religious_attendance_model3_moderation$se_chronotype_interval_beta.urban_rural_area_beta

View(coef_se_issp_2007_religious_attendance_model3_moderation)

ranef(issp_2007_religious_attendance_model3_moderation)$country
ranef_issp_2007_religious_attendance_model3_moderation <- data.frame(ranef(issp_2007_religious_attendance_model3_moderation)$country)
ranef_issp_2007_religious_attendance_model3_moderation <- subset(ranef_issp_2007_religious_attendance_model3_moderation, select = c("chronotype_interval_beta", "chronotype_interval_beta.urban_rural_area_beta"))
ranef_issp_2007_religious_attendance_model3_moderation$country <- row.names(ranef_issp_2007_religious_attendance_model3_moderation)
names(ranef_issp_2007_religious_attendance_model3_moderation)[names(ranef_issp_2007_religious_attendance_model3_moderation)=="chronotype_interval_beta"] <- "ranef_chronotype_interval_beta"
names(ranef_issp_2007_religious_attendance_model3_moderation)[names(ranef_issp_2007_religious_attendance_model3_moderation)=="chronotype_interval_beta.urban_rural_area_beta"] <- "ranef_chronotype_interval_beta.urban_rural_area_beta"

se_ranef_issp_2007_religious_attendance_model3_moderation <- data.frame(arm::se.ranef(issp_2007_religious_attendance_model3_moderation)$country)
se_ranef_issp_2007_religious_attendance_model3_moderation <- subset(se_ranef_issp_2007_religious_attendance_model3_moderation, select = c("chronotype_interval_beta", "chronotype_interval_beta.urban_rural_area_beta"))
se_ranef_issp_2007_religious_attendance_model3_moderation$country <- row.names(se_ranef_issp_2007_religious_attendance_model3_moderation)
names(se_ranef_issp_2007_religious_attendance_model3_moderation)[names(se_ranef_issp_2007_religious_attendance_model3_moderation)=="chronotype_interval_beta"] <- "se_chronotype_interval_beta"
names(se_ranef_issp_2007_religious_attendance_model3_moderation)[names(se_ranef_issp_2007_religious_attendance_model3_moderation)=="chronotype_interval_beta.urban_rural_area_beta"] <- "se_chronotype_interval_beta.urban_rural_area_beta"

ranef_se_issp_2007_religious_attendance_model3_moderation <- merge(ranef_issp_2007_religious_attendance_model3_moderation, se_ranef_issp_2007_religious_attendance_model3_moderation, by="country")

ranef_se_issp_2007_religious_attendance_model3_moderation$chrono.cl.90 <- ranef_se_issp_2007_religious_attendance_model3_moderation$ranef_chronotype_interval_beta - 1.645*ranef_se_issp_2007_religious_attendance_model3_moderation$se_chronotype_interval_beta
ranef_se_issp_2007_religious_attendance_model3_moderation$chrono.cu.90 <- ranef_se_issp_2007_religious_attendance_model3_moderation$ranef_chronotype_interval_beta + 1.645*ranef_se_issp_2007_religious_attendance_model3_moderation$se_chronotype_interval_beta
ranef_se_issp_2007_religious_attendance_model3_moderation$chrono.cl.95 <- ranef_se_issp_2007_religious_attendance_model3_moderation$ranef_chronotype_interval_beta - 1.96*ranef_se_issp_2007_religious_attendance_model3_moderation$se_chronotype_interval_beta
ranef_se_issp_2007_religious_attendance_model3_moderation$chrono.cu.95 <- ranef_se_issp_2007_religious_attendance_model3_moderation$ranef_chronotype_interval_beta + 1.96*ranef_se_issp_2007_religious_attendance_model3_moderation$se_chronotype_interval_beta
ranef_se_issp_2007_religious_attendance_model3_moderation$chrono.cl.99 <- ranef_se_issp_2007_religious_attendance_model3_moderation$ranef_chronotype_interval_beta - 2.576*ranef_se_issp_2007_religious_attendance_model3_moderation$se_chronotype_interval_beta
ranef_se_issp_2007_religious_attendance_model3_moderation$chrono.cu.99 <- ranef_se_issp_2007_religious_attendance_model3_moderation$ranef_chronotype_interval_beta + 2.576*ranef_se_issp_2007_religious_attendance_model3_moderation$se_chronotype_interval_beta

ranef_se_issp_2007_religious_attendance_model3_moderation$interaction.cl.90 <- ranef_se_issp_2007_religious_attendance_model3_moderation$ranef_chronotype_interval_beta.urban_rural_area_beta - 1.645*ranef_se_issp_2007_religious_attendance_model3_moderation$se_chronotype_interval_beta.urban_rural_area_beta
ranef_se_issp_2007_religious_attendance_model3_moderation$interaction.cu.90 <- ranef_se_issp_2007_religious_attendance_model3_moderation$ranef_chronotype_interval_beta.urban_rural_area_beta + 1.645*ranef_se_issp_2007_religious_attendance_model3_moderation$se_chronotype_interval_beta.urban_rural_area_beta
ranef_se_issp_2007_religious_attendance_model3_moderation$interaction.cl.95 <- ranef_se_issp_2007_religious_attendance_model3_moderation$ranef_chronotype_interval_beta.urban_rural_area_beta - 1.96*ranef_se_issp_2007_religious_attendance_model3_moderation$se_chronotype_interval_beta.urban_rural_area_beta
ranef_se_issp_2007_religious_attendance_model3_moderation$interaction.cu.95 <- ranef_se_issp_2007_religious_attendance_model3_moderation$ranef_chronotype_interval_beta.urban_rural_area_beta + 1.96*ranef_se_issp_2007_religious_attendance_model3_moderation$se_chronotype_interval_beta.urban_rural_area_beta
ranef_se_issp_2007_religious_attendance_model3_moderation$interaction.cl.99 <- ranef_se_issp_2007_religious_attendance_model3_moderation$ranef_chronotype_interval_beta.urban_rural_area_beta - 2.576*ranef_se_issp_2007_religious_attendance_model3_moderation$se_chronotype_interval_beta.urban_rural_area_beta
ranef_se_issp_2007_religious_attendance_model3_moderation$interaction.cu.99 <- ranef_se_issp_2007_religious_attendance_model3_moderation$ranef_chronotype_interval_beta.urban_rural_area_beta + 2.576*ranef_se_issp_2007_religious_attendance_model3_moderation$se_chronotype_interval_beta.urban_rural_area_beta

View(ranef_se_issp_2007_religious_attendance_model3_moderation)

#### Online Appendices, Table 28: Average Marginal Effects of Chronotype’s Main Effect for Each Country ####

library(marginaleffects)

summary(marginaleffects(issp_2007_religious_attendance_model3_moderation, variables = c("chronotype_interval_beta"), 
                        newdata = datagrid(urban_rural_area_beta = 1, 
                                           sex_beta = 0,
                                           age_beta = 0,
                                           degree_beta = 0,
                                           income_beta = 0,
                                           PARTY_LR_beta = 0,
                                           political_interest_beta = 0,
                                           country = "Finland")))

summary(marginaleffects(issp_2007_religious_attendance_model3_moderation, variables = c("chronotype_interval_beta"), 
                        newdata = datagrid(urban_rural_area_beta = 1, 
                                           sex_beta = 0,
                                           age_beta = 0,
                                           degree_beta = 0,
                                           income_beta = 0,
                                           PARTY_LR_beta = 0,
                                           political_interest_beta = 0,
                                           country = "Greece")))

summary(marginaleffects(issp_2007_religious_attendance_model3_moderation, variables = c("chronotype_interval_beta"), 
                        newdata = datagrid(urban_rural_area_beta = 1, 
                                           sex_beta = 0,
                                           age_beta = 0,
                                           degree_beta = 0,
                                           income_beta = 0,
                                           PARTY_LR_beta = 0,
                                           political_interest_beta = 0,
                                           country = "Ireland")))

summary(marginaleffects(issp_2007_religious_attendance_model3_moderation, variables = c("chronotype_interval_beta"), 
                        newdata = datagrid(urban_rural_area_beta = 1, 
                                           sex_beta = 0,
                                           age_beta = 0,
                                           degree_beta = 0,
                                           income_beta = 0,
                                           PARTY_LR_beta = 0,
                                           political_interest_beta = 0,
                                           country = "Mexico")))

summary(marginaleffects(issp_2007_religious_attendance_model3_moderation, variables = c("chronotype_interval_beta"), 
                        newdata = datagrid(urban_rural_area_beta = 1, 
                                           sex_beta = 0,
                                           age_beta = 0,
                                           degree_beta = 0,
                                           income_beta = 0,
                                           PARTY_LR_beta = 0,
                                           political_interest_beta = 0,
                                           country = "the Netherlands")))

summary(marginaleffects(issp_2007_religious_attendance_model3_moderation, variables = c("chronotype_interval_beta"), 
                        newdata = datagrid(urban_rural_area_beta = 1, 
                                           sex_beta = 0,
                                           age_beta = 0,
                                           degree_beta = 0,
                                           income_beta = 0,
                                           PARTY_LR_beta = 0,
                                           political_interest_beta = 0,
                                           country = "New Zealand")))

summary(marginaleffects(issp_2007_religious_attendance_model3_moderation, variables = c("chronotype_interval_beta"), 
                        newdata = datagrid(urban_rural_area_beta = 1, 
                                           sex_beta = 0,
                                           age_beta = 0,
                                           degree_beta = 0,
                                           income_beta = 0,
                                           PARTY_LR_beta = 0,
                                           political_interest_beta = 0,
                                           country = "the Philippines")))

summary(marginaleffects(issp_2007_religious_attendance_model3_moderation, variables = c("chronotype_interval_beta"), 
                        newdata = datagrid(urban_rural_area_beta = 1,
                                           sex_beta = 0,
                                           age_beta = 0,
                                           degree_beta = 0,
                                           income_beta = 0,
                                           PARTY_LR_beta = 0,
                                           political_interest_beta = 0,
                                           country = "Russia")))

summary(marginaleffects(issp_2007_religious_attendance_model3_moderation, variables = c("chronotype_interval_beta"), 
                        newdata = datagrid(urban_rural_area_beta = 1, 
                                           sex_beta = 0,
                                           age_beta = 0,
                                           degree_beta = 0,
                                           income_beta = 0,
                                           PARTY_LR_beta = 0,
                                           political_interest_beta = 0,
                                           country = "South Korea")))

summary(marginaleffects(issp_2007_religious_attendance_model3_moderation, variables = c("chronotype_interval_beta"), 
                        newdata = datagrid(urban_rural_area_beta = 1,
                                           sex_beta = 0,
                                           age_beta = 0,
                                           degree_beta = 0,
                                           income_beta = 0,
                                           PARTY_LR_beta = 0,
                                           political_interest_beta = 0,
                                           country = "Switzerland")))

#### Manuscript, Figure 1: Relationship of chronotype with political ideology (left) and religious attendance (right). ####

table(issp_2007$chronotype_interval_beta, exclude = NULL)
chr_values <- unique(issp_2007$chronotype_interval_beta)
chr_values <- data.frame(chr_values)
chr_values$chr_values <- sort(chr_values$chr_values)

library(marginaleffects)

predicted_values_ideology <- predictions(issp_2007_left_right_model3, 
                                         newdata = datagrid(country = NA,
                                                            chronotype_interval_beta = c(chr_values$chr_values)), include_random = FALSE)

predicted_values_ideology <- subset(predicted_values_ideology, select = c("predicted", "std.error", "chronotype_interval_beta"))
predicted_values_ideology$Effect <- "Fixed Effect"
predicted_values_ideology$Sample <- "Pooled"
predicted_values_ideology$Significance <- "Insignificant"
predicted_values_ideology$Size <- 1

predicted_values_ideology_FI <- predictions(issp_2007_left_right_model3, 
                                            newdata = datagrid(country = "Finland",
                                                               chronotype_interval_beta = c(chr_values$chr_values)))
predicted_values_ideology_FI <- subset(predicted_values_ideology_FI, select = c("predicted", "std.error", "chronotype_interval_beta"))
predicted_values_ideology_FI$Effect <- "Predicted Effect"
predicted_values_ideology_FI$Sample <- "Finland"
predicted_values_ideology_FI$Significance <- "Insignificant"
predicted_values_ideology_FI$Size <- 0.25

predicted_values_ideology_GR <- predictions(issp_2007_left_right_model3, 
                                            newdata = datagrid(country = "Greece",
                                                               chronotype_interval_beta = c(chr_values$chr_values)))
predicted_values_ideology_GR <- subset(predicted_values_ideology_GR, select = c("predicted", "std.error", "chronotype_interval_beta"))
predicted_values_ideology_GR$Effect <- "Predicted Effect"
predicted_values_ideology_GR$Sample <- "Greece"
predicted_values_ideology_GR$Significance <- "Marginally Significant"
predicted_values_ideology_GR$Size <- 0.5

predicted_values_ideology_IE <- predictions(issp_2007_left_right_model3, 
                                            newdata = datagrid(country = "Ireland",
                                                               chronotype_interval_beta = c(chr_values$chr_values)))
predicted_values_ideology_IE <- subset(predicted_values_ideology_IE, select = c("predicted", "std.error", "chronotype_interval_beta"))
predicted_values_ideology_IE$Effect <- "Predicted Effect"
predicted_values_ideology_IE$Sample <- "Ireland"
predicted_values_ideology_IE$Significance <- "Insignificant"
predicted_values_ideology_IE$Size <- 0.25

predicted_values_ideology_MX <- predictions(issp_2007_left_right_model3, 
                                            newdata = datagrid(country = "Mexico",
                                                               chronotype_interval_beta = c(chr_values$chr_values)))
predicted_values_ideology_MX <- subset(predicted_values_ideology_MX, select = c("predicted", "std.error", "chronotype_interval_beta"))
predicted_values_ideology_MX$Effect <- "Predicted Effect"
predicted_values_ideology_MX$Sample <- "Mexico"
predicted_values_ideology_MX$Significance <- "Insignificant"
predicted_values_ideology_MX$Size <- 0.25

predicted_values_ideology_NL <- predictions(issp_2007_left_right_model3, 
                                            newdata = datagrid(country = "the Netherlands",
                                                               chronotype_interval_beta = c(chr_values$chr_values)))
predicted_values_ideology_NL <- subset(predicted_values_ideology_NL, select = c("predicted", "std.error", "chronotype_interval_beta"))
predicted_values_ideology_NL$Effect <- "Predicted Effect"
predicted_values_ideology_NL$Sample <- "the Netherlands"
predicted_values_ideology_NL$Significance <- "Insignificant"
predicted_values_ideology_NL$Size <- 0.25

predicted_values_ideology_NZ <- predictions(issp_2007_left_right_model3, 
                                            newdata = datagrid(country = "New Zealand",
                                                               chronotype_interval_beta = c(chr_values$chr_values)))
predicted_values_ideology_NZ <- subset(predicted_values_ideology_NZ, select = c("predicted", "std.error", "chronotype_interval_beta"))
predicted_values_ideology_NZ$Effect <- "Predicted Effect"
predicted_values_ideology_NZ$Sample <- "New Zealand"
predicted_values_ideology_NZ$Significance <- "Marginally Significant"
predicted_values_ideology_NZ$Size <- 0.5

predicted_values_ideology_PH <- predictions(issp_2007_left_right_model3, 
                                            newdata = datagrid(country = "the Philippines",
                                                               chronotype_interval_beta = c(chr_values$chr_values)))
predicted_values_ideology_PH <- subset(predicted_values_ideology_PH, select = c("predicted", "std.error", "chronotype_interval_beta"))
predicted_values_ideology_PH$Effect <- "Predicted Effect"
predicted_values_ideology_PH$Sample <- "the Philippines"
predicted_values_ideology_PH$Significance <- "Insignificant"
predicted_values_ideology_PH$Size <- 0.25

predicted_values_ideology_RU <- predictions(issp_2007_left_right_model3, 
                                            newdata = datagrid(country = "Russia",
                                                               chronotype_interval_beta = c(chr_values$chr_values)))
predicted_values_ideology_RU <- subset(predicted_values_ideology_RU, select = c("predicted", "std.error", "chronotype_interval_beta"))
predicted_values_ideology_RU$Effect <- "Predicted Effect"
predicted_values_ideology_RU$Sample <- "Russia"
predicted_values_ideology_RU$Significance <- "Significant"
predicted_values_ideology_RU$Size <- 0.5

predicted_values_ideology_SK <- predictions(issp_2007_left_right_model3, 
                                            newdata = datagrid(country = "South Korea",
                                                               chronotype_interval_beta = c(chr_values$chr_values)))
predicted_values_ideology_SK <- subset(predicted_values_ideology_SK, select = c("predicted", "std.error", "chronotype_interval_beta"))
predicted_values_ideology_SK$Effect <- "Predicted Effect"
predicted_values_ideology_SK$Sample <- "South Korea"
predicted_values_ideology_SK$Significance <- "Insignificant"
predicted_values_ideology_SK$Size <- 0.25

predicted_values_ideology_CH <- predictions(issp_2007_left_right_model3, 
                                            newdata = datagrid(country = "Switzerland",
                                                               chronotype_interval_beta = c(chr_values$chr_values)))
predicted_values_ideology_CH <- subset(predicted_values_ideology_CH, select = c("predicted", "std.error", "chronotype_interval_beta"))
predicted_values_ideology_CH$Effect <- "Predicted Effect"
predicted_values_ideology_CH$Sample <- "Switzerland"
predicted_values_ideology_CH$Significance <- "Significant"
predicted_values_ideology_CH$Size <- 0.5

predicted_values_ideology_PLOT_DATA <- dplyr::bind_rows(predicted_values_ideology,
                                                        predicted_values_ideology_FI,
                                                        predicted_values_ideology_GR,
                                                        predicted_values_ideology_IE,
                                                        predicted_values_ideology_MX,
                                                        predicted_values_ideology_NL,
                                                        predicted_values_ideology_NZ,
                                                        predicted_values_ideology_PH,
                                                        predicted_values_ideology_RU,
                                                        predicted_values_ideology_SK,
                                                        predicted_values_ideology_CH)

str(predicted_values_ideology_PLOT_DATA$Size)
predicted_values_ideology_PLOT_DATA$Size <- factor(predicted_values_ideology_PLOT_DATA$Size)

ideology_PLOT <- ggplot(data = predicted_values_ideology_PLOT_DATA, aes(x=chronotype_interval_beta, y=predicted, size = Size)) +
  geom_line(aes(linetype=Sample)) +
  scale_linetype_manual(values=c("dotted", # FI
                                 "longdash", # GR 
                                 "dotted", # IR
                                 "dotted", # MX
                                 "longdash", # NZ
                                 "solid", # POOLED
                                 "solid", # RU
                                 "dotted", # SK
                                 "solid", # CH
                                 "dotted", # NL
                                 "dotted")) + theme_classic() +
  scale_size_manual(values=c(0.35, 0.5, 2)) +
  theme(legend.position="none") +
  theme(axis.text.x=element_text(colour="black", size = 12), axis.text.y=element_text(colour="black", size = 12)) +
  theme(axis.title=element_text(size=14, colour="black", face="bold"), legend.text=element_text(size=14), legend.title=element_text(size=14)) +
  labs(    x = "\n Chronotype (Standardized) \n (Eveningness ← → Morningness)\n", 
           y = "\n Predicted Values of Left-Right Ideological Placement (Standardized) \n (Left ← → Right) \n",
           title = "") +
  scale_y_continuous(breaks=c(-0.6, -0.5, -0.4, -0.3, -0.2, -0.1, 0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8), limits = c(-0.6, 0.8))


ideology_PLOT

table(issp_2007$chronotype_interval_beta, exclude = NULL)
chr_values <- unique(issp_2007$chronotype_interval_beta)
chr_values <- data.frame(chr_values)
chr_values$chr_values <- sort(chr_values$chr_values)

library(marginaleffects)

predicted_values_religiosity <- predictions(issp_2007_religiosity_model4, 
                                            newdata = datagrid(country = NA,
                                                               chronotype_interval_beta = c(chr_values$chr_values)), include_random = FALSE)

predicted_values_religiosity <- subset(predicted_values_religiosity, select = c("predicted", "std.error", "chronotype_interval_beta"))
predicted_values_religiosity$Effect <- "Fixed Effect"
predicted_values_religiosity$Sample <- "Pooled"
predicted_values_religiosity$Significance <- "Significant"
predicted_values_religiosity$Size <- 1

predicted_values_religiosity_FI <- predictions(issp_2007_religiosity_model4, 
                                               newdata = datagrid(country = "Finland",
                                                                  chronotype_interval_beta = c(chr_values$chr_values)))
predicted_values_religiosity_FI <- subset(predicted_values_religiosity_FI, select = c("predicted", "std.error", "chronotype_interval_beta"))
predicted_values_religiosity_FI$Effect <- "Predicted Effect"
predicted_values_religiosity_FI$Sample <- "Finland"
predicted_values_religiosity_FI$Significance <- "Insignificant"
predicted_values_religiosity_FI$Size <- 0.25

predicted_values_religiosity_GR <- predictions(issp_2007_religiosity_model4, 
                                               newdata = datagrid(country = "Greece",
                                                                  chronotype_interval_beta = c(chr_values$chr_values)))
predicted_values_religiosity_GR <- subset(predicted_values_religiosity_GR, select = c("predicted", "std.error", "chronotype_interval_beta"))
predicted_values_religiosity_GR$Effect <- "Predicted Effect"
predicted_values_religiosity_GR$Sample <- "Greece"
predicted_values_religiosity_GR$Significance <- "Significant"
predicted_values_religiosity_GR$Size <- 0.5

predicted_values_religiosity_IE <- predictions(issp_2007_religiosity_model4, 
                                               newdata = datagrid(country = "Ireland",
                                                                  chronotype_interval_beta = c(chr_values$chr_values)))
predicted_values_religiosity_IE <- subset(predicted_values_religiosity_IE, select = c("predicted", "std.error", "chronotype_interval_beta"))
predicted_values_religiosity_IE$Effect <- "Predicted Effect"
predicted_values_religiosity_IE$Sample <- "Ireland"
predicted_values_religiosity_IE$Significance <- "Significant"
predicted_values_religiosity_IE$Size <- 0.5

predicted_values_religiosity_MX <- predictions(issp_2007_religiosity_model4, 
                                               newdata = datagrid(country = "Mexico",
                                                                  chronotype_interval_beta = c(chr_values$chr_values)))
predicted_values_religiosity_MX <- subset(predicted_values_religiosity_MX, select = c("predicted", "std.error", "chronotype_interval_beta"))
predicted_values_religiosity_MX$Effect <- "Predicted Effect"
predicted_values_religiosity_MX$Sample <- "Mexico"
predicted_values_religiosity_MX$Significance <- "Significant"
predicted_values_religiosity_MX$Size <- 0.5

predicted_values_religiosity_NL <- predictions(issp_2007_religiosity_model4, 
                                               newdata = datagrid(country = "the Netherlands",
                                                                  chronotype_interval_beta = c(chr_values$chr_values)))
predicted_values_religiosity_NL <- subset(predicted_values_religiosity_NL, select = c("predicted", "std.error", "chronotype_interval_beta"))
predicted_values_religiosity_NL$Effect <- "Predicted Effect"
predicted_values_religiosity_NL$Sample <- "the Netherlands"
predicted_values_religiosity_NL$Significance <- "Significant"
predicted_values_religiosity_NL$Size <- 0.5

predicted_values_religiosity_NZ <- predictions(issp_2007_religiosity_model4, 
                                               newdata = datagrid(country = "New Zealand",
                                                                  chronotype_interval_beta = c(chr_values$chr_values)))
predicted_values_religiosity_NZ <- subset(predicted_values_religiosity_NZ, select = c("predicted", "std.error", "chronotype_interval_beta"))
predicted_values_religiosity_NZ$Effect <- "Predicted Effect"
predicted_values_religiosity_NZ$Sample <- "New Zealand"
predicted_values_religiosity_NZ$Significance <- "Insignificant"
predicted_values_religiosity_NZ$Size <- 0.25

predicted_values_religiosity_PH <- predictions(issp_2007_religiosity_model4, 
                                               newdata = datagrid(country = "the Philippines",
                                                                  chronotype_interval_beta = c(chr_values$chr_values)))
predicted_values_religiosity_PH <- subset(predicted_values_religiosity_PH, select = c("predicted", "std.error", "chronotype_interval_beta"))
predicted_values_religiosity_PH$Effect <- "Predicted Effect"
predicted_values_religiosity_PH$Sample <- "the Philippines"
predicted_values_religiosity_PH$Significance <- "Significant"
predicted_values_religiosity_PH$Size <- 0.5

predicted_values_religiosity_RU <- predictions(issp_2007_religiosity_model4, 
                                               newdata = datagrid(country = "Russia",
                                                                  chronotype_interval_beta = c(chr_values$chr_values)))
predicted_values_religiosity_RU <- subset(predicted_values_religiosity_RU, select = c("predicted", "std.error", "chronotype_interval_beta"))
predicted_values_religiosity_RU$Effect <- "Predicted Effect"
predicted_values_religiosity_RU$Sample <- "Russia"
predicted_values_religiosity_RU$Significance <- "Insignificant"
predicted_values_religiosity_RU$Size <- 0.25

predicted_values_religiosity_SK <- predictions(issp_2007_religiosity_model4, 
                                               newdata = datagrid(country = "South Korea",
                                                                  chronotype_interval_beta = c(chr_values$chr_values)))
predicted_values_religiosity_SK <- subset(predicted_values_religiosity_SK, select = c("predicted", "std.error", "chronotype_interval_beta"))
predicted_values_religiosity_SK$Effect <- "Predicted Effect"
predicted_values_religiosity_SK$Sample <- "South Korea"
predicted_values_religiosity_SK$Significance <- "Significant"
predicted_values_religiosity_SK$Size <- 0.5

predicted_values_religiosity_CH <- predictions(issp_2007_religiosity_model4, 
                                               newdata = datagrid(country = "Switzerland",
                                                                  chronotype_interval_beta = c(chr_values$chr_values)))
predicted_values_religiosity_CH <- subset(predicted_values_religiosity_CH, select = c("predicted", "std.error", "chronotype_interval_beta"))
predicted_values_religiosity_CH$Effect <- "Predicted Effect"
predicted_values_religiosity_CH$Sample <- "Switzerland"
predicted_values_religiosity_CH$Significance <- "Insignificant"
predicted_values_religiosity_CH$Size <- 0.25

predicted_values_religiosity_PLOT_DATA <- dplyr::bind_rows(predicted_values_religiosity,
                                                           predicted_values_religiosity_FI,
                                                           predicted_values_religiosity_GR,
                                                           predicted_values_religiosity_IE,
                                                           predicted_values_religiosity_MX,
                                                           predicted_values_religiosity_NL,
                                                           predicted_values_religiosity_NZ,
                                                           predicted_values_religiosity_PH,
                                                           predicted_values_religiosity_RU,
                                                           predicted_values_religiosity_SK,
                                                           predicted_values_religiosity_CH)

str(predicted_values_religiosity_PLOT_DATA$Size)
predicted_values_religiosity_PLOT_DATA$Size <- factor(predicted_values_religiosity_PLOT_DATA$Size)

religiosity_PLOT <- ggplot(data = predicted_values_religiosity_PLOT_DATA, aes(x=chronotype_interval_beta, y=predicted, size = Size)) +
  geom_line(aes(linetype=Sample)) +
  scale_linetype_manual(values=c("dotted", # FI
                                 "solid", # GR 
                                 "solid", # IR
                                 "solid", # MX
                                 "dotted", # NZ
                                 "solid", # POOLED
                                 "dotted", # RU
                                 "solid", # SK
                                 "dotted", # CH
                                 "solid", # NL
                                 "solid")) + theme_classic() +
  scale_size_manual(values=c(0.35, 0.5, 2)) +
  theme(axis.text.x=element_text(colour="black", size = 12), axis.text.y=element_text(colour="black", size = 12)) +
  theme(axis.title=element_text(size=14, colour="black", face="bold"), legend.text=element_text(size=14), legend.title=element_text(size=14)) +
  labs(    x = "\n Chronotype (Standardized) \n (Eveningness ← → Morningness)\n", 
           y = "\n Predicted Values of Religious Attendance (Standardized) \n (Lesser Religiosity ← → Higher Religiosity)\n",
           title = "") +
  theme(legend.position="none") +
  scale_y_continuous(breaks=c(-0.6, -0.5, -0.4, -0.3, -0.2, -0.1, 0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8), limits = c(-0.6, 0.8))

religiosity_PLOT

predicted_figures <- ggpubr::ggarrange(ideology_PLOT, religiosity_PLOT,
                                 ncol=2)
predicted_figures

ggsave(filename = "predicted_figures_300dpi.jpg", plot = predicted_figures, width = 20, height = 10, dpi = 700)
ggsave(filename = "predicted_figures_300dpi.png", plot = predicted_figures, width = 20, height = 10, dpi = 700)
ggsave(filename = "predicted_figures_300dpi.jpeg", plot = predicted_figures, width = 20, height = 10, dpi = 700)



